如何将值从BroadcastReceiver传递到另一个活动

时间:2013-04-07 11:24:10

标签: android sms broadcastreceiver

经过几个小时思考如何将一个值从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> 

我已经仔细阅读了我在this link找到的教程。

它说“我们在类本身声明一个BroadcastReciever而不是onResume()方法中的清单,这样当应用程序在前台返回时,它应该收到短信。”

我认为“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

1 个答案:

答案 0 :(得分:1)

在日志中:

  

您的内容必须包含其属性为的ListView   'android.R.id.list'

表示如果您要延长ListView

,则需要在android:id="@android:id/list"布局中将activity_smsreceiver ID更改为ListActivity