如何将数据传递到广播接收器?(How to pass data to BroadcastReceiv

2019-06-23 09:46发布

我所要做的就是到我的应用程序发送的消息的号码,被传递到BraodcastReceiver ......但到目前为止,无论是我得到空或广播接收器只是简单的崩溃..

这里是从类的CreateMessage我的广播接收器的意图...

        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("phN", phoneNo);
        intent.putExtras(bundle);
        startActivity(intent);

而在广播接收器(SmsReceiver类),我试图抓住意图这样的..

public void onReceive(Context context, Intent intent) {
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();
    try{
    //receiveNumbers = intent.getExtras().get("phN").toString();
        String  receiveNumbers = intent.getStringExtra("phN");
        Toast.makeText(context, receiveNumbers, Toast.LENGTH_LONG).show();
    }catch(Exception e){
        e.printStackTrace();
    }

我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.AEM_n"
      android:versionCode="2" android:versionName="2.0">
    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>    
    <uses-permission  android:name="android.permission.SEND_SMS"/>
    <uses-permission  android:name="android.permission.RECEIVE_SMS"/>

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
        <activity android:name=".new_menu"
                  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:label="@string/app_name"
            android:name=".SetEvent" >
            <intent-filter>
                <action android:name="com.AEM_n.SETEVENT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            </activity>
            <activity
            android:label="@string/app_name"
            android:name=".AddParticipants" />
            <activity
            android:label="@string/app_name"
            android:name=".CreateMessage" />
            <activity
            android:label="@string/app_name"
            android:name=".DataBaseClass" />
            <activity 
            android:label="@string/app_name"
            android:name=".IntentReceiver"/>
            <activity 
            android:label="@string/app_name"
            android:name=".SmsReceiver"
            />
            <receiver android:name=".SmsReceiver" android:exported="true">
            <intent-filter android:priority="999">
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
            </receiver>
    </application>

</manifest>

我已经尝试给出了#1所有的答案......但没有用...

请你们告诉我,我错了...谢谢!

错误日志:

04-07 02:34:02.770: ERROR/AndroidRuntime(25593): FATAL EXCEPTION: main
04-07 02:34:02.770: ERROR/AndroidRuntime(25593): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.myapp.myaction (has extras) } in com.AEM_n.SmsReceiver@405a07c8
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at android.os.Handler.handleCallback(Handler.java:587)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at android.os.Looper.loop(Looper.java:123)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at android.app.ActivityThread.main(ActivityThread.java:3701)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at java.lang.reflect.Method.invoke(Method.java:507)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at dalvik.system.NativeStart.main(Native Method)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593): Caused by: java.lang.NullPointerException
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at com.AEM_n.SmsReceiver.onReceive(SmsReceiver.java:37)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):     ... 9 more

Answer 1:

继@Jason的榜样......我这样做...

在MainActivity或从那里你想从发送意图的任何活动

Intent intent = new Intent("my.action.string");
intent.putExtra("extra", phoneNo); \\ phoneNo is the sent Number
sendBroadcast(intent);

然后在我的SmsReceiver类我这样做

public void onReceive(Context context, Intent intent) {
  String action = intent.getAction();

  Log.i("Receiver", "Broadcast received: " + action);

  if(action.equals("my.action.string")){
     String state = intent.getExtras().getString("extra");

  }
}

而在我manifest.xml中添加"my.action.string"虽然这是一个选项..

<receiver android:name=".SmsReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        <action android:name="my.action.string" />
        <!-- and some more actions if you want -->
    </intent-filter>
</receiver>

工作就像魅力!



Answer 2:

你开始的Activity ,而不是广播Intent 。 尝试改变

startActivity(intent);

sendBroadcast(intent);

更新

您可以设置任何动作,没有组件名称的Intent 。 尝试创建意图如下:

Intent intent = new Intent(context, YourReceiver.class);
intent.putExtra("phN", phoneNo);
sendBroadcast(intent);


Answer 3:

你会发送一个广播,如下所示:

Intent intent = new Intent(action);
intent.putExtra("phN", phoneNo);
sendBroadcast(intent);

action参数是一个String是关联到您注册的动作BroadcastReceiver与。 所以,如果您注册的接收器,如下所示:

MyBroadcastReceiver receiver = new MyBroadcastReceiver();
registerReceiver( receiver, new IntentFilter( "com.myapp.myaction" ) );

那么action将是"com.myapp.myaction"



Answer 4:

你的问题其实很简单。 这就够了变化的onReceive()代码,就像它:

public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    Log.i("Receiver", "Broadcast received: " + action);

   if(action.equals("my.action.string")){
       String state = bundle.getString("phN");

   }
}


Answer 5:

使用sendbroadcast代替startactivity.it的将工作.. !!



文章来源: How to pass data to BroadcastReceiver?