2012-09-25 123 views
2

我從我的課的一個發送廣播:廣播接收器的onReceive不叫

// Sending broadcast from my db helper constructor 
    public DBHelper(Context ctx){ 
    this.ctx = ctx; 
    Intent intent = new Intent(); 
    intent.setAction("custom.alert.abc"); 
    intent.putExtra("key",77);ctx.sendBroadcast(intent); 




    //registering the broadcast in one of my activities oncreate 
    IntentFilter alaramfilter = new IntentFilter(); 
    alaramfilter.addAction("custom.alert.abc"); 
    registerReceiver(alarmreceiver, alaramfilter); 

    //and declaring it 
    private final BroadcastReceiver alarmreceiver = new BroadcastReceiver() { 
@Override 
public void onReceive(Context context, Intent intent) { 
    System.out.println("Broadcast received inside on receive"); 
    final String action = intent.getAction(); 
    if(action.equals("custom.alert.abc")) { 
     Log.d("CPSnippetMain","My custom broadcast called"); 
     try { 
      System.out.println("Broadcast received"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
}; 

的問題是,雖然構造函數被調用,並廣播發送,的onReceive不叫。

任何幫助表示讚賞。

+1

您註冊了嗎? – nullpotent

+0

是的,編輯我的問題。我錯過了包括它 – user1400538

+0

'alaramfilter'它是什麼? – nullpotent

回答

3

您的訂單sendbroadcastregister是重要的。 首先註冊,然後做一個sendBroadcast。

0

註冊boardcast

private BroadcastReceiver receiver_; 

@Overrride 
public void onCreate(Bundle savedInstanceState){ 



IntentFilter filter_data = new IntentFilter(); 
filter_data .addAction("SOME_ACTION"); 


receiver_= new BroadcastReceiver() { 
@Override 
public void onReceive(Context context, Intent intent) { 
    //do something based on the intent's action 
} 
} 

registerReceiver(receiver_, filter_data); 

和unregisterReceiver在破壞methord

unregisterReceiver(receiver_);