繁体   English   中英

多种自定义广播(如何处理)

[英]Multiple custom broadcasting (how to handle)

我想向我的应用添加自定义广播接收器。 我有3种方法应该进行广播。 在我的onReceive方法中,我想确定广播的是哪种方法。 我有3种这样的方法

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

这是我的broadcastIntent方法

public void broadcastIntent(Intent intent){
    sendBroadcast(intent);
}

在我的onReceive方法中,我使用getFlags()方法从意图中获取标志值,并通过if,else将其发送。 但这不起作用。 欢迎提出任何改进建议。

第一个问题是ypu没有为您的意图指定目标。 您可以使用意图过滤器和建议的操作,例如rodkarom或直接指定接收者的类(请参见我的示例)。 在这两种情况下,您都需要在AndroidManifest.xml中声明广播接收器,或者在运行时进行注册(请参阅rodkarom的答案以获取示例)。

方法addFlags用于指定Intent的一些内部属性(例如,与新任务中的该intent对应的启动活动),因此您不能将其用于自己的数据。 可能的标志列表在setFlags方法的文档中。

您可以使用putExtra实现您的目标:

// 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) {
            // ...
        }
        // ...
    }
}

您还可以使用操作来标识每个Intent对象。

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

我找到了解决方法,并想到了共享代码。 这是我主要活动中针对2种不同方法所做的广播。

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>

希望这对其他人遇到类似问题也能有所帮助。 非常感谢在这里发布答案的每个人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM