广播发送者和接收者对

时间:2012-01-09 05:43:06

标签: android broadcastreceiver

在一个档案中,我正在发送广播。

Intent Message1 = new Intent(this, MyClass.class);
Message1.putExtra("hello", false);
sendBroadcast(Message1);

在接收方,我有

myreceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        if (intent.getExtra.equals("hello") {
            [...]
        }
    }
    [...]
}

关键是接收者必须有意图与广播公司的“你好”的额外匹配才能继续。我可以这样做吗?

1 个答案:

答案 0 :(得分:1)

为什么要添加额外的而不使用intent filter + action(这是接收者的主要想法)?

我想这样的事情

IntentFilter filter = new IntentFilter("hello"); // More complicated action maybe?
yourContext.registerReceiver(new myreceiver(), filter);

并在onReceive中:

 public void onReceive(Context context, Intent intent) 
 {
    String action = intent.getAction();

    if (action.equals("hello")) 
    {
        // go on....
    }
 }
相关问题