0

I am creating an app that only needs to listen for SMS messages with the text "FIND" in them from all the numbers. I have written the code but it's crashing whenever any SMS is received. Please help!

public class SMSReceiver extends BroadcastReceiver {
Context Context;

@Override
public void onReceive(Context context, Intent intent) {
    this.Context = context;

    if (intent.getAction()
            .equals("android.provider.Telephony.SMS_RECEIVED")) {
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        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]);
                    String msgBody = msgs[i].getMessageBody();
                    if (msgBody == "FIND") {
                        Toast toast = Toast.makeText(context,
                                "SMS RECEIVED", Toast.LENGTH_LONG);
                        toast.show();
                    }
                }
            } catch (Exception e) {
                // Log.d("Exception caught",e.getMessage());
            }
        }
    }
}
}

Here is the Logcat:

02-18 13:01:23.547: W/dalvikvm(1264): threadid=1: thread exiting with uncaught exception (group=0xb5f624f0)
02-18 13:01:23.547: E/AndroidRuntime(1264): FATAL EXCEPTION: main
02-18 13:01:23.547: E/AndroidRuntime(1264): java.lang.RuntimeException: Unable to instantiate receiver pv.ilostmyphone.listener.SmsListener: java.lang.ClassNotFoundException: pv.ilostmyphone.listener.SmsListener in loader dalvik.system.PathClassLoader[/data/app/pv.ilostmyphone-1.apk]
02-18 13:01:23.547: E/AndroidRuntime(1264):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1773)
02-18 13:01:23.547: E/AndroidRuntime(1264):     at android.app.ActivityThread.access$2400(ActivityThread.java:117)
02-18 13:01:23.547: E/AndroidRuntime(1264):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
02-18 13:01:23.547: E/AndroidRuntime(1264):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-18 13:01:23.547: E/AndroidRuntime(1264):     at android.os.Looper.loop(Looper.java:130)
02-18 13:01:23.547: E/AndroidRuntime(1264):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-18 13:01:23.547: E/AndroidRuntime(1264):     at java.lang.reflect.Method.invokeNative(Native Method)
02-18 13:01:23.547: E/AndroidRuntime(1264):     at java.lang.reflect.Method.invoke(Method.java:507)
02-18 13:01:23.547: E/AndroidRuntime(1264):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-18 13:01:23.547: E/AndroidRuntime(1264):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-18 13:01:23.547: E/AndroidRuntime(1264):     at dalvik.system.NativeStart.main(Native Method)
02-18 13:01:23.547: E/AndroidRuntime(1264): Caused by: java.lang.ClassNotFoundException: pv.ilostmyphone.listener.SmsListener in loader dalvik.system.PathClassLoader[/data/app/pv.ilostmyphone-1.apk]
02-18 13:01:23.547: E/AndroidRuntime(1264):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
02-18 13:01:23.547: E/AndroidRuntime(1264):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
02-18 13:01:23.547: E/AndroidRuntime(1264):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-18 13:01:23.547: E/AndroidRuntime(1264):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1764)
02-18 13:01:23.547: E/AndroidRuntime(1264):     ... 10 more
02-18 13:02:13.797: E/AndroidRuntime(1306):     ... 10 more

Here is my android manifest-

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pv.ilostmyphone"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name=".SMSReceiver" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" >
            </action>
        </intent-filter>
    </receiver>
</application>

</manifest>
Parth
  • 123
  • 6
  • pls provide the stacktrace of the exception message. regarding the background case - thats what broadcast receivers are for ;) – stamanuel Feb 18 '14 at 07:36
  • Is this another of your account ?http://stackoverflow.com/users/3318657/shahabi – user3317558 Feb 18 '14 at 07:59
  • @user3317558- No it is not. – Parth Feb 18 '14 at 10:35
  • Yeah I did that. Could this be a problem with the emulators? I am using the built in emulators in eclipse and sending an SMS using telnet. – Parth Feb 21 '14 at 07:01
  • I did. The logcat is the same. – Parth Feb 21 '14 at 07:05
  • Yeah I changed the class name, I'll try rebuilding the project. I think I found the problem, http://stackoverflow.com/questions/4619049/unable-to-instantiate-receiver-in-broadcastreceiver-sms – Parth Feb 21 '14 at 07:21
  • I got the app from not crashing by making a package ending with .Broadcast and then putting the receiver name as ".Broadcast.SMSReceiver". But my Toast message is still not appearing. Should I delete this question as it's not really answered and ask a new one? – Parth Feb 21 '14 at 07:47
  • Oh wait it's working! Didn't fix the msgBody == "FIND" party. Thanks anyways! – Parth Feb 21 '14 at 07:51

3 Answers3

0

Here's my code from a similar app

public class SMSReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        if (bundle != null) {
            Object[] pdusObj = (Object[]) bundle.get("pdus");
            SmsMessage[] messages = new SmsMessage[pdusObj.length];

            // getting SMS information from Pdu.
            for (int i = 0; i < pdusObj.length; i++) {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
            }

            for (SmsMessage currentMessage : messages) {
                String sender = currentMessage.getDisplayOriginatingAddress();
                String message = currentMessage.getDisplayMessageBody();
                messageReceived(sender, message);
            }

        }
    }

    protected void messageReceived(String sender, String message){
        if(message.equals("FIND"){
            //DO whatever here
            }
    }
}
Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

You are comparing String in wrong way, In Java .equals() method is used to compare two strings, So change

if (msgBody == "FIND")   

to

if (msgBody.equals("FIND") ) 

But for better result I would like to suggest you to try startsWith() method ,

if (msgBody.startsWith("FIND") )

The reason for the above line is that I am sure your msg contains more words.

user3317558
  • 482
  • 2
  • 13
0

I fixed this problem by remaking my project. I made a MainActivity.java and then created another package under src named in this format "(package name).Broadcast". I created the SMSReceiver.java in this package. Then, I changed the receiver in the manifest to this:

    <receiver android:name=".Broadcast.SMSReceiver" >
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED" >
            </action>
        </intent-filter>
    </receiver>
Parth
  • 123
  • 6