• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Troubles with SMS receiver app

Kizari

Lurker
Hey there, I'm working on an app that I want to receive SMS messages from my phone and send the body of the message off to my server.

To provide some background I have been programming in C++ for a good five years now, but I am still quite new to java, thankfully it's not too different so I haven't had many problems with that. I'm also very new to android development, I am currently working with android studio on windows.

Basically what I'm trying to make is an app without a GUI that runs in the background, will trigger when an SMS is received by the device, will process the SMS and then delete the SMS, that's basically all I want it to do a this point. I found some decent resources while poking around on google, but I haven't had any luck getting it to work as of yet.

I'm a little unsure of how android apps work though. In the templates there's always an activity, but from what I've read this is only required for apps with a GUI, please correct me if I'm wrong there, so I created a blank project. I created a class, SmsListener.java that contains the following code:

Code:
public class SmsListener extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
        {
            Bundle bundle = intent.getExtras();
            SmsMessage[] msgs = null;
            String msg_from;

            if(bundle != null)
            {
                try
                {
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length];
                    for(int i=0; i<msgs.length; i++)
                    {
                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                        msg_from = msgs[i].getOriginatingAddress();
                        String msgBody = msgs[i].getMessageBody();
                    }
                }
                catch(Exception e)
                {

                }
            }
        }
    }
}

And here's the relevant part of the manifest:

Code:
<uses-permission android:name="android.permission.RECEIVE_SMS" />

    <application android:allowBackup="true" android:label="@string/app_name"
        android:icon="@mipmap/ic_launcher" android:theme="@style/AppTheme">

        <receiver android:name=".SmsListener">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

I'm doing fine working the emulator, and I'm able to send SMS to the phone using telnet. I have a couple of problems though, I'm not even entirely sure the app is running at the moment, I tried adding code to send a notification that triggers inside
Code:
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))

But nothing happens at all when an SMS is received. I'm not sure if that's because the BroadcastReceiver is inactive, or if the code is just wrong. Do I need another class or a MainActivity for the BroadcastReceiver to continue running?

Any help on the matter would be greatly appreciated. Thank you for your time.
 
Back
Top Bottom