註冊多個接收器,使用同一種過濾器
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TextView textOne = new TextView(this);
final TextView textTwo = new TextView(this);
LinearLayout container = new LinearLayout(this);
container.setOrientation(LinearLayout.VERTICAL);
container.addView(textOne);
container.addView(textTwo);
setContentView(container);
final String Action = "FilterString";
IntentFilter filter = new IntentFilter(Action);
BroadcastReceiver receiverOne = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
textOne.setText("textOne changed");
}
};
registerReceiver(receiverOne, filter);
BroadcastReceiver receiverTwo = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
textTwo.setText("textTwo changed");
}
};
registerReceiver(receiverTwo, filter);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Intent intent = new Intent(Action);
sendBroadcast(intent);
}
}, 2000);
}
}