Android 如何将数据传递给 BroadcastReceiver?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10032480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass data to BroadcastReceiver?
提问by Ahmed S. Durrani
What I am trying to do is that the numbers to which my application sends messages to, are passed to the BraodcastReceiver...but so far either I am getting null or BroadcastReceiver just simply crashes..
我想要做的是,我的应用程序向其发送消息的数字被传递到 BraodcastReceiver ......但到目前为止,我要么得到 null 要么 BroadcastReceiver 只是简单地崩溃了..
here is my intent for BroadcastReceiver from CreateMessage class...
这是我对来自 CreateMessage 类的 BroadcastReceiver 的意图......
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("phN", phoneNo);
intent.putExtras(bundle);
startActivity(intent);
And in BroadcastReceiver (SmsReceiver class) I am trying to catch intent like this..
在 BroadcastReceiver(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();
}
My Manifest File:
我的清单文件:
<?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>
I have already tried all the answers given Stackoverflow...But no use...
我已经尝试了所有给定 Stackoverflow 的答案......但没有用......
Please guys tell me where I am going wrong...Thanks!
请大家告诉我哪里出错了...谢谢!
Error Log:
错误日志:
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
回答by Ahmed S. Durrani
Following @Jason 's example...I did this...
按照@Jason 的例子......我这样做了......
In MainActivity or any activity from where you want to send intent from
在 MainActivity 或您要从中发送意图的任何活动中
Intent intent = new Intent("my.action.string");
intent.putExtra("extra", phoneNo); \ phoneNo is the sent Number
sendBroadcast(intent);
and then in my SmsReceiver Class I did this
然后在我的 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");
}
}
And in manifest.xml I added "my.action.string"
though it was an option..
在 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>
worked like charm!!
像魅力一样工作!!
回答by pepyakin
You starting an Activity
instead of broadcasting Intent
.
Try to change
你开始一个Activity
而不是广播Intent
。尝试改变
startActivity(intent);
to
到
sendBroadcast(intent);
UPDATE:
更新:
You set no action and no component name to the Intent
.
Try create intent as following:
您没有设置任何操作和组件名称到Intent
. 尝试创建意图如下:
Intent intent = new Intent(context, YourReceiver.class);
intent.putExtra("phN", phoneNo);
sendBroadcast(intent);
回答by Jason Robinson
You would send a broadcast like so:
你会像这样发送广播:
Intent intent = new Intent(action);
intent.putExtra("phN", phoneNo);
sendBroadcast(intent);
The action
parameter is a String
that correlates to the action you registered the BroadcastReceiver
with. So if you registered your receiver like so:
该action
参数是String
相关于您注册的动作BroadcastReceiver
用。因此,如果您像这样注册接收器:
MyBroadcastReceiver receiver = new MyBroadcastReceiver();
registerReceiver( receiver, new IntentFilter( "com.myapp.myaction" ) );
then action
would be "com.myapp.myaction"
那么action
将是"com.myapp.myaction"
回答by BinMan1
Your problem is actually very simple. It's enough that change onReceive() code just like it :
你的问题其实很简单。像这样更改 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");
}
}
回答by saksham agarwal
use sendbroadcast instead of startactivity.it will work..!!
使用 sendbroadcast 而不是 startactivity.it 会工作..!!