在 Broadcast 中傳送訊息
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final TextView text = new TextView(this);
setContentView(text);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle message = intent.getExtras();
String value = message.getString("KeyOne");
text.setText(value);
}
};
final String Action = "FilterStringOne";
IntentFilter filter = new IntentFilter(Action);
registerReceiver(receiver, filter);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
Bundle message = new Bundle();
message.putString("KeyOne", "ValueOne");
Intent intent = new Intent(Action);
intent.putExtras(message);
sendBroadcast(intent);
}
}, 2000);
}
}