2012-01-09 103 views
0

在一个文件上,我发送了broardcast。广播发送者和接收者对

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") { 
      [...] 
     } 
    } 
    [...] 
} 

的关键是receiever必须有意向的额外比赛广播公司的“你好”,以继续进行。我可以这样做吗?

回答

1

为什么要额外和不使用意图过滤器+动作(这是接收器的主要思想)?

我会以这样的

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.... 
    } 
}