Android 多个自定义广播(如何处理)
我想在我的应用程序中添加自定义广播接收器。我有三种方法可以播放。在我的onReceive方法中,我想确定广播的方法。我有三种这样的方法Android 多个自定义广播(如何处理),android,broadcastreceiver,Android,Broadcastreceiver,我想在我的应用程序中添加自定义广播接收器。我有三种方法可以播放。在我的onReceive方法中,我想确定广播的方法。我有三种这样的方法 public void method01(View v){ int flag = 1; Intent intent = new Intent(); intent.addFlags(flag); broadcastIntent(intent); } public void method02(){ int flag = 2;
public void method01(View v){
int flag = 1;
Intent intent = new Intent();
intent.addFlags(flag);
broadcastIntent(intent);
}
public void method02(){
int flag = 2;
Intent intent = new Intent();
intent.addFlags(flag);
broadcastIntent(intent);
}
public void method03(){
int flag = 3;
Intent intent = new Intent();
intent.addFlags(flag);
broadcastIntent(intent);
}
这就是我的方法
public void broadcastIntent(Intent intent){
sendBroadcast(intent);
}
在onReceive方法中,我使用getFlags()方法从intent获取标志值,并通过if、else发送。但这是行不通的。欢迎提出任何改进建议 第一个问题是ypu没有为您的意图指定目标。您可以使用意图过滤器和rodkarom建议的操作,或者直接指定接收者的类(参见我的示例)。在这两种情况下,您都需要在中声明广播接收器,或者在运行时注册它(请参阅rodkarom的答案中的示例) 方法
addFlags
用于指定Intent的一些内部属性(如在新任务中与此意图对应的启动活动),因此您不能将其用于自己的数据。可能的标志列表在方法的文档中
您可以使用以下方法来实现您的目标:
// an Activity is just an example
public class SenderActivity extends Activity {
// ...
void method01() {
int flag = 1;
Intent intent = new Intent(getApplicationContext(), Receiver.class); // any Context is acceptable here
intent.putExtra(MyReceiver.EXTRA_FLAG, flag); // any string will do well, you just need it to be the same here and in getExtra later
sendBroadcast(intent);
}
}
public class MyReceiver extends BroadcastReceiver {
public static final String EXTRA_FLAG = "your.package.name.EXTRA_FLAG";
// and in onReceive
public void onReceive (Context context, Intent intent) {
int flag = intent.getIntExtra(EXTRA_FLAG, someDefaultValue);
if (flag == 1) {
// ...
}
// ...
}
}
第一个问题是ypu没有为您的意图指定目标。您可以使用意图过滤器和rodkarom建议的操作,或者直接指定接收者的类(参见我的示例)。在这两种情况下,您都需要在中声明广播接收器,或者在运行时注册它(请参阅rodkarom的答案中的示例) 方法
addFlags
用于指定Intent的一些内部属性(如在新任务中与此意图对应的启动活动),因此您不能将其用于自己的数据。可能的标志列表在方法的文档中
您可以使用以下方法来实现您的目标:
// an Activity is just an example
public class SenderActivity extends Activity {
// ...
void method01() {
int flag = 1;
Intent intent = new Intent(getApplicationContext(), Receiver.class); // any Context is acceptable here
intent.putExtra(MyReceiver.EXTRA_FLAG, flag); // any string will do well, you just need it to be the same here and in getExtra later
sendBroadcast(intent);
}
}
public class MyReceiver extends BroadcastReceiver {
public static final String EXTRA_FLAG = "your.package.name.EXTRA_FLAG";
// and in onReceive
public void onReceive (Context context, Intent intent) {
int flag = intent.getIntExtra(EXTRA_FLAG, someDefaultValue);
if (flag == 1) {
// ...
}
// ...
}
}
您还可以使用操作来标识每个意图对象
String action1 = "first_sender";
String action2 = "second_sender";
Intent createIntent01(){
Intent intent01 = new Intent();
intent01.setAction(action1);
return intent01;
}
Intent createIntent02(){
Intent intent02 = new Intent();
intent01.setAction(action2);
return intent02;
}
在您的onReceive
方法中,您可以使用意图的getAction()
方法来检查发送了哪个操作。这是为了防止您尚未使用操作
[[编辑]]
要注册BroadcastReceiver
,您需要定义IntentFilter
,并注册希望通过这种方式接收的操作:
mBroadcastReceiver broadcastReceiver = new mBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(action1);
intentFilter.addAction(action2);
registerReceiver(broadcastReceiver,intentFilter);
class mBroadcastReceiver extends BroadcastReceiver{
public void onReceive(Context arg0, Intent arg1){
String action = arg1.getAction();
if(action.equals(action1)){
//do something
}else if(action.equals(action2)){
//do something else
}
}
您还可以使用操作来标识每个意图对象
String action1 = "first_sender";
String action2 = "second_sender";
Intent createIntent01(){
Intent intent01 = new Intent();
intent01.setAction(action1);
return intent01;
}
Intent createIntent02(){
Intent intent02 = new Intent();
intent01.setAction(action2);
return intent02;
}
在您的onReceive
方法中,您可以使用意图的getAction()
方法来检查发送了哪个操作。这是为了防止您尚未使用操作
[[编辑]]
要注册BroadcastReceiver
,您需要定义IntentFilter
,并注册希望通过这种方式接收的操作:
mBroadcastReceiver broadcastReceiver = new mBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(action1);
intentFilter.addAction(action2);
registerReceiver(broadcastReceiver,intentFilter);
class mBroadcastReceiver extends BroadcastReceiver{
public void onReceive(Context arg0, Intent arg1){
String action = arg1.getAction();
if(action.equals(action1)){
//do something
}else if(action.equals(action2)){
//do something else
}
}
我找到了实现这一点的方法,并考虑分享代码。这是我在主要活动中针对两种不同方法所做的广播
public void method1(View view){
Intent intent = new Intent();
intent.setAction("method1");
sendBroadcast(intent);
}
public void method2(View view){
Intent intent = new Intent();
intent.setAction("method2");
sendBroadcast(intent);
}
我就是这样收到的
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Broadcast Intent Detected.",Toast.LENGTH_LONG).show();
}
这是我在舱单上登记的方式
<receiver android:name="Receiver" >
<intent-filter>
<action android:name="method1" >
</action>
<action android:name="method2" >
</action>
</intent-filter>
</receiver>
希望这将有助于任何其他人提出类似的问题。非常感谢所有在这里发布答案的人 我找到了实现这一点的方法,并考虑共享代码。这是我在主要活动中针对两种不同方法所做的广播
public void method1(View view){
Intent intent = new Intent();
intent.setAction("method1");
sendBroadcast(intent);
}
public void method2(View view){
Intent intent = new Intent();
intent.setAction("method2");
sendBroadcast(intent);
}
我就是这样收到的
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Broadcast Intent Detected.",Toast.LENGTH_LONG).show();
}
这是我在舱单上登记的方式
<receiver android:name="Receiver" >
<intent-filter>
<action android:name="method1" >
</action>
<action android:name="method2" >
</action>
</intent-filter>
</receiver>
希望这将有助于任何其他人提出类似的问题。非常感谢所有在这里发布答案的人 您是否从代码段中省略了意图目标(如指定组件/操作)?换句话说,你能在你的接收者身上接收到意图吗?你的问题不清楚,抱歉不清楚。。不,我根本没有从我的接收者那里收到它。您是否从代码片段中省略了意图目标(如指定组件/操作)?换句话说,你能在你的接收者身上接收到意图吗?你的问题不清楚,抱歉不清楚。。不,我根本没有从我的接收者那里收到它。首先感谢你的回答,在onReceive方法中,他们在EXTRA_标志下显示错误,我也尝试使用intent.EXTRA,但没有成功。(我在不同的活动中有onReceive()和广播方法,所以我想额外的_标志无法解决)只需调用intent.putExtra(“intValue”,1)在发送广播和调用intent.getIntExtra之前,在您的接收器中首先感谢您的回答,在onReceive方法中,它们在额外的_标志下显示错误,我尝试使用intent.EXTRA,但是没有成功。(我在不同的活动中有onReceive()和广播方法,所以我想额外的_标志无法解决)只需调用intent.putExtra(“intValue”,1)在发送广播和调用intent.getIntExtra之前,在你的receiverHi@rodkarom感谢你的回答,如果你有任何来源,我可以提供这个的详细版本。嗨@rodkarom谢谢你的回答,如果你有任何来源,可以提供我这个的详细版本。