0

经过几个小时的思考,我几乎放弃了如何将值从 BroadcastReceiver 传递到另一个 Activity。

这是我目前在 BroadcastReceiver 类中的代码:

package com.example.smsTest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

       Bundle extras = intent.getExtras();
       if (extras == null)
       return;

       Object[] pdus = (Object[]) extras.get("pdus");
       for (int i = 0; i < pdus.length; i++) {
          SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
          String sender = SMessage.getOriginatingAddress();
          String body = SMessage.getMessageBody().toString();

         // A custom Intent that will used as another Broadcast
         Intent in = new Intent("SmsMessage.intent.MAIN").
         putExtra("get_msg", sender+":"+body);

         // To display a Toast whenever there is an SMS.
         Toast.makeText(context,body,Toast.LENGTH_LONG).show();

         //You can place your check conditions here(on the SMS or the sender)            
         //and then send another broadcast 
         context.sendBroadcast(in);

        // This is used to abort the broadcast and can be used to silently
        // process incoming message and prevent it from further being 
        // broadcasted. Avoid this, as this is not the way to program an app.
        this.abortBroadcast();
        }
     }
 }

这是 SMSReceiverActivity 类的代码:

package com.example.smsTest;

import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;

public class SMSReceiverActivity extends ListActivity {
    private BroadcastReceiver mIntentReceiver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smsreceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();

        IntentFilter intentFilter = new IntentFilter("SmsMessage.intent.MAIN");
        mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String msg = intent.getStringExtra("get_msg");

            //Process the sms format and extract body &amp; phoneNumber
            msg = msg.replace("\n", "");
            String body = msg.substring(msg.lastIndexOf(":")+1, msg.length());
            String pNumber = msg.substring(0,msg.lastIndexOf(":"));

            //Add it to the list or do whatever you wish to
            TextView text = (TextView) findViewById(R.id.editText1);
            text.setText(body);
        }
    };

    this.registerReceiver(mIntentReceiver, intentFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        this.unregisterReceiver(this.mIntentReceiver);
    }

}

我还在 AndroidManifest.xml 中定义了活动,如下所示:

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

    <uses-sdk android:minSdkVersion="4" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SMSTest"
                  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=".SMSReceiverActivity"></activity>
        <receiver android:name=".SmsReceiver">
            <intent-filter android:priority="2147483647">
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>
    </application>
    <uses-permission android:name="android.permission.SEND_SMS">
    </uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_SMS">
    </uses-permission>

</manifest> 

我已密切关注在此链接上找到的教程。

它在那里说“我们在类本身中声明了一个 BroadcastReciever,而不是在 onResume() 方法中的清单,以便当应用程序返回前台时,它应该接收 SMS。”

我假设如果“SMSReceiverActivity”处于活动状态,它将接收来自 SmsReceiver(即 BroadcastReceiver)的消息。我尝试创建一个按钮来从我的 SMSTest.java 打开 SMSReceiverActivity,如下所示:

btnMsgRec.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        Intent myIntent = new Intent(view.getContext(), SMSReceiverActivity.class);
        startActivityForResult(myIntent, 0);
    }

});

但是当我单击按钮时,应用程序崩溃了。

如果有人可以帮助我,我将非常感激。如何将 BroadcastReceiver 中的值作为新 Activity 传递给 SMSReceiverActivity.java。

谢谢

更新:

这是我单击“打开短信接收器”按钮后的 logcat:

04-07 11:38:16.878: D/AndroidRuntime(5269): Shutting down VM
04-07 11:38:16.878: W/dalvikvm(5269): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-07 11:38:16.908: E/AndroidRuntime(5269): FATAL EXCEPTION: main
04-07 11:38:16.908: E/AndroidRuntime(5269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.smsTest/com.example.smsTest.SMSReceiverActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-07 11:38:16.908: E/AndroidRuntime(5269):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at android.os.Looper.loop(Looper.java:137)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at java.lang.reflect.Method.invoke(Method.java:511)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at dalvik.system.NativeStart.main(Native Method)
04-07 11:38:16.908: E/AndroidRuntime(5269): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
04-07 11:38:16.908: E/AndroidRuntime(5269):     at android.app.ListActivity.onContentChanged(ListActivity.java:243)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at android.app.Activity.setContentView(Activity.java:1881)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at com.example.smsTest.SMSReceiverActivity.onCreate(SMSReceiverActivity.java:17)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at android.app.Activity.performCreate(Activity.java:5104)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-07 11:38:16.908: E/AndroidRuntime(5269):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-07 11:38:16.908: E/AndroidRuntime(5269):     ... 11 more
4

1 回答 1

1

如日志:

您的内容必须有一个 ID 属性为“android.R.id.list”的 ListView

意味着如果要扩展,则需要将ListViewid 更改为android:id="@android:id/list"in layoutactivity_smsreceiverListActivity

于 2013-04-07T11:45:38.500 回答