如何从活动传递价值的广播接收器? [英] How to pass value from activity to broadcastreceiver?
问题描述
我开发利用广播接收器我有问题,从活动的广播接收器接收值一个应用程序,请告诉我如何从一个活动值发送到广播接收器,以及如何从活动的广播接收器收到的价值,我试图像下面code,但它不工作
我的code
在活动方
意向意图=新意图(my.action.string);
//intent.setAction(\"IncomCallBroadCast);
intent.putExtra(联系,PHNO);
sendBroadcast(意向);
在广播接收器侧
字符串行动= intent.getAction();
Log.i(接收器,广播收到:+动作);
如果(action.equals(my.action.string)){
触点= intent.getExtras()的getString(联系)。
Log.e(,接触);
}
表现
<作用机器人:名字=my.action.string/>
在主活动中使用这种code,开始广播接收器
意向意图=新意图(package.action.string);
intent.putExtra(额外,PHONENO); \\\\ PHONENO是发送号码
sendBroadcast(意向);
在广播reciver使用code
公共无效的onReceive(上下文的背景下,意图意图){
字符串行动= intent.getAction(); Log.i(接收器,广播收到:+动作); 如果(action.equals(package.action.string)){
字符串状态= intent.getExtras()的getString(额外)。 }
}
请注意:DONOT忘记申报您的广播接收器清单文件
<接收机器人:名字=机器人SmsReceiver:启用=真正的>
&所述;意图滤光器>
<作用机器人:名字=android.provider.Telephony.SMS_RECEIVED/>
<作用机器人:名字=package.action.string/>
<! - 和一些行动,如果你想 - >
&所述; /意图滤光器>
< /接收器>
I am developing one application using broadcastreceiver i have problem with receive value in broadcast receiver from activity,please tell me how to send value from activity to broadcast receiver and how to receive value in broadcast receiver from activity ,i am trying like below code but its not working
my code in activity side
Intent intent = new Intent("my.action.string");
//intent.setAction("IncomCallBroadCast");
intent.putExtra("contact",phNo);
sendBroadcast(intent);
in broadcastreceiver side
String action = intent.getAction();
Log.i("Receiver", "Broadcast received: " + action);
if(action.equals("my.action.string")){
contact = intent.getExtras().getString("contact");
Log.e("",contact );
}
manifest
<action android:name="my.action.string"/>
In main activity use this code to start broadcast receiver
Intent intent = new Intent("package.action.string");
intent.putExtra("extra", phoneNo); \\ phoneNo is the sent Number
sendBroadcast(intent);
In the broadcast reciver use this code
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("Receiver", "Broadcast received: " + action);
if(action.equals("package.action.string")){
String state = intent.getExtras().getString("extra");
}
}
NOTE : Donot forget to declare your Broadcast receiver in manifest file
<receiver android:name=".SmsReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<action android:name="package.action.string" />
<!-- and some more actions if you want -->
</intent-filter>
</receiver>
这篇关于如何从活动传递价值的广播接收器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!