Android 具有多个动作的广播接收器
Android Broadcast receiver with multiple actions
我应该如何实现广播接收器和过滤器,以便它可以响应多个 intents
。
private BroadcastReceiver myReceiver;
IntentFilter myFilter = new IntentFilter();
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);
来自我的片段:
Intent i = new Intent("first"); sendBroadcast(i);
Intent i = new Intent("second"); sendBroadcast(i);
谢谢
@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
}
}
}
我应该如何实现广播接收器和过滤器,以便它可以响应多个 intents
。
private BroadcastReceiver myReceiver;
IntentFilter myFilter = new IntentFilter();
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);
来自我的片段:
Intent i = new Intent("first"); sendBroadcast(i);
Intent i = new Intent("second"); sendBroadcast(i);
谢谢
@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
}
}
}