当收到来自特定号码的消息时,从短信广播接收机开始活动

时间:2013-02-26 05:33:06

标签: android android-intent android-activity

我已经关注了许多链接,但仍无法找到问题的解决方案。基本上,我有2个班UsingBroadcastReceiverStartAction。当我收到来自特定号码的消息时,我需要开始StartAction活动。为此,我实现了以下代码。 更新的代码: -

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();
    }
}

}

当我在模拟器和设备上运行此代码时,我收到ForceClose错误消息。 logcat如下:

       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

另外,我需要访问UsingBroadcastReceiver类中StartAction类的String主体的值。这是正确的方法吗?

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

我的清单如下:

 <?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 个答案:

答案 0 :(得分:1)

      <receiver android:name=".UsingBroadcastReceiver">

你没有插入“。”在接收器

@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);
 }

}     }