简体   繁体   English

Android广播接收器具有多个动作

[英]Android Broadcast receiver with multiple actions

How should I implement a broadcast receiver & filter so that it can response to multiple intents . 我应该如何实现广播接收器和过滤器,以便它可以响应多个intents

private BroadcastReceiver myReceiver;
IntentFilter myFilter = new IntentFilter();

onCreate(): 的onCreate():

    myFilter.addAction("first");
    myFilter.addAction("second");

    myReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // do different actions according to the intent
            }
        };

    registerReceiver(myReceiver, myFilter);

from my fragment: 来自我的片段:

Intent i = new Intent("first"); sendBroadcast(i);

Intent i = new Intent("second"); sendBroadcast(i);

Thanks 谢谢

@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if(action != null) {
        if(action.equals("action1") {
            // CODE
        } else if (action.equals("action2") {
            // CODE
        }
    }
}

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

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