1

多くのリンクをたどりましたが、それでも問題の解決策が見つかりませんでした。基本的に、私は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。これは正しい方法ですか?

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>
4

1 に答える 1

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

}}

于 2013-02-26T05:40:10.257 に答える