starting an activity from sms broadcast reciever w

2019-05-29 08:13发布

I have followed many links but still could not find a solution to my problem. Basically, i have 2 classes UsingBroadcastReceiver and StartAction. When i receive a message from a particular number, i need to start the StartAction activity. For this, i have implemented the following code. Updated code:-

public class UsingBroadcastReceiver extends BroadcastReceiver {

final SmsManager sms = SmsManager.getDefault();

String body = null;
String no = "15555215558";


@Override
public void onReceive(Context context, Intent intent) {
    // Parse the SMS.
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String str = "";
    if (bundle != null)
    {
        // Retrieve the SMS.
        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]);

            if(msgs[i].getOriginatingAddress().equals(no))
            {
                body = msgs[i].getMessageBody();

            str += "SMS from " + msgs[i].getOriginatingAddress();
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";

            Intent open = new Intent(context, StartAction.class);
            open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            open.putExtra("body", body);
            context.startActivity(open);
            }
        }
        // Display the SMS as Toast.
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }
}

}

When i run this code on the emulator as well as the device, i get the ForceClose error message. logcat is as follows:

       02-26 11:40:50.047: E/AndroidRuntime(689): FATAL EXCEPTION: main
02-26 11:40:50.047: E/AndroidRuntime(689): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.usingbroadcastreceiver/com.example.usingbroadcastreceiver.UsingBroadcastReceiver}: java.lang.ClassCastException: com.example.usingbroadcastreceiver.UsingBroadcastReceiver
02-26 11:40:50.047: E/AndroidRuntime(689):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
02-26 11:40:50.047: E/AndroidRuntime(689):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-26 11:40:50.047: E/AndroidRuntime(689):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-26 11:40:50.047: E/AndroidRuntime(689):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-26 11:40:50.047: E/AndroidRuntime(689):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 11:40:50.047: E/AndroidRuntime(689):  at android.os.Looper.loop(Looper.java:123)
02-26 11:40:50.047: E/AndroidRuntime(689):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-26 11:40:50.047: E/AndroidRuntime(689):  at java.lang.reflect.Method.invokeNative(Native Method)
02-26 11:40:50.047: E/AndroidRuntime(689):  at java.lang.reflect.Method.invoke(Method.java:507)
02-26 11:40:50.047: E/AndroidRuntime(689):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-26 11:40:50.047: E/AndroidRuntime(689):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-26 11:40:50.047: E/AndroidRuntime(689):  at dalvik.system.NativeStart.main(Native Method)
02-26 11:40:50.047: E/AndroidRuntime(689): Caused by: java.lang.ClassCastException: com.example.usingbroadcastreceiver.UsingBroadcastReceiver
02-26 11:40:50.047: E/AndroidRuntime(689):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-26 11:40:50.047: E/AndroidRuntime(689):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
02-26 11:40:50.047: E/AndroidRuntime(689):  ... 11 more

Also, i need to access the value of String body of the UsingBroadcastReceiver class in the StartAction class. Is this the correct way to do it?

StartAction start = new StartAction();
            start.msgBody = body;

My manifest is as follows:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.usingbroadcastreceiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.usingbroadcastreceiver.UsingBroadcastReceiver"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
            android:name="com.example.usingbroadcastreceiver.StartAction"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.usingbroadcastreceiver.StartAction" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

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

</manifest>

1条回答
我命由我不由天
2楼-- · 2019-05-29 09:11
      <receiver android:name=".UsingBroadcastReceiver">

you do not insert "." in receiver

@Override
public void onReceive(Context context, Intent intent) {

        String address1 = null;

        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                address1 = messages[i].getOriginatingAddress();

            }

if (address1==your compareable number ){
  Intent open = new Intent(context, StartAction.class);
        open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(open);
 }

} }

查看更多
登录 后发表回答