Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将值从BroadcastReceiver传递到另一个活动_Android_Sms_Broadcastreceiver - Fatal编程技术网

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

Android 如何将值从BroadcastReceiver传递到另一个活动,android,sms,broadcastreceiver,Android,Sms,Broadcastreceiver,在思考如何将值从广播接收器传递到另一个活动几个小时后,我几乎放弃了 以下是我目前在BroadcastReceiver类中拥有的代码: package com.example.smsTest; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telepho

在思考如何将值从广播接收器传递到另一个活动几个小时后,我几乎放弃了

以下是我目前在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();
        }
     }
 }
我还在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> 
btnMsgRec.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        Intent myIntent = new Intent(view.getContext(), SMSReceiverActivity.class);
        startActivityForResult(myIntent, 0);
    }

});
但当我点击按钮时,应用程序崩溃了

如果有人能在这件事上帮助我,我将不胜感激。如何将值作为新活动从BroadcastReceiverActivity传递到SMSReceiverActivity.java

多谢各位

更新:

这是我点击“打开短信接收器”按钮后的日志:

如日志所示:

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


这意味着您需要将
ListView
id更改为
android:id=“@android:id/list”
中的
activity\u smsreceiver
布局,如果您正在扩展
ListActivity

,您可以调整我对这个问题的回答,如果有帮助的话,也许可以对其进行升级:)嗨,非常感谢。我快到了。我现在可以将值从
BroadcastReceiver
设置为
SMSReceiverActivity
。现在如何将值显示到ListView而不是文本视图。如何更改此行:
TextView text=(TextView)findViewById(R.id.editText1);text.setText(正文)很抱歉没有问题。@jaypabs:现在您是否在textview中获取值?是的,我现在在textview中获取值。我现在想要的是将它从BroadcastReceiver接收到的每条消息附加到listview,而不是文本视图。您能帮我吗?@jaypabs:当然,请尝试更改您的设置以在listview中显示数据。该值不会转到listview。它显示在文本视图中。如果正确,请查看我的活动\u smsreceiver.xml。给你
btnMsgRec.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        Intent myIntent = new Intent(view.getContext(), SMSReceiverActivity.class);
        startActivityForResult(myIntent, 0);
    }

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