註冊多個接收器,使用不同過濾器
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);
BroadcastReceiver receiverOne = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
textOne.setText("textOne changed");
}
};
final String ActionOne = "FilterStringOne";
IntentFilter filterOne = new IntentFilter(ActionOne);
registerReceiver(receiverOne, filterOne);
BroadcastReceiver receiverTwo = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
textTwo.setText("textTwo changed");
}
};
final String ActionTwo = "FilterStringTwo";
IntentFilter filterTwo = new IntentFilter(ActionTwo);
registerReceiver(receiverTwo, filterTwo);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Intent intent = new Intent(ActionOne);
sendBroadcast(intent);
}
}, 2000);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Intent intent = new Intent(ActionTwo);
sendBroadcast(intent);
}
}, 5000);
}
}