如何才能正确地从其他活动中取消注册接收器?

时间:2013-09-02 13:04:19

标签: android android-activity reference broadcastreceiver

我无法将我的接收器注销到另一个活动中。这是:

在我的启动活动A中启动/注册BroadcastReceiver。

活动A:

public class ActivityA extends Activity {
   private PowerButtonReceiver mPowerButtonReceiver = null;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      if (getPowerButtonReceiver() == null)
      {
            mPowerButtonReceiver(new PowerButtonReceiver());                                            
            registerReceiver(mPowerButtonReceiver, new IntentFilter("android.intent.action.SCREEN_ON"));
      }         
   }

public void unregister() {
    try {
        unregisterReceiver(mPowerButtonReceiver);
    } catch (NullPointerException e) {
        e.printStackTrace();
    }       
    if (mPowerButtonReceiver == null) {
        Log.i(TAG,"unregistered PowerButtonReceiver!");
    }       
}

然后我在我的接收器的onReceive()方法中启动Activity B. 在此之后我进入onCreate() - Activity B的方法,做我的东西,在onCreate()结束时,我想取消注册BroadcastReceiver:

活动B:( - > android:launchMode =“singleTop”)

public class ActivityB extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);                 

    setContentView(R.layout.activity_camera);       

    [...do my stuff...]

//un-register PowerButtonReceiver --> call in Activity A

    MainActivity m = new MainActivity();
    m.getParent();
    m.unregister();         
}   

我的最后一行开始调用unregister(),但是Receiver为null,看起来它没有得到它的引用。接收方也没有注册,仍然在听。我很确定我的错误是在参考/调用中;但我不明白该怎么做:/

我在这里缺少什么?有人能帮助我吗?

3 个答案:

答案 0 :(得分:1)

通过intent.putExtra()方法将您的活动A上下文传递给活动B.然后,而不是MainActivity m = new MainActivity();MainActivity m = ((MainActivity)(passedContext));

答案 1 :(得分:1)

我已经成功尝试了这样的事情,看看这是否有帮助:我有一个HeadSetTesTActivity,一个Headsetconnectorreciever,ActivityB。
HEADSETTESTACTIVITY.JAVA
       `public class HeadsettestActivity extends Activity {     /** 在第一次创建活动时调用。 * /     Headsetconnectorreciever r;

static HeadsettestActivity activity;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    activity=this;
    Log.d("Inside activity ", "before broadcast reciever registered");
    r = new Headsetconnectorreciever();
    registerReceiver(r, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
    Log.d("Inside activity ", "after broadcast reciever registered");
}
public  void unregister() {
    try {
        if(r!=null){
            unregisterReceiver(r);
            Log.i("HeadsettestActivity","Inside unregister ");
        }
    } catch (NullPointerException e) {
        e.printStackTrace();
    }       
    if (r == null) {
        Log.i("HeadsettestActivity","unregistered PowerButtonReceiver!");
    }
}

}`

<强> Headsetconnectorreciever
`public class Headsetconnectorreciever扩展BroadcastReceiver {      private boolean headsetConnected = false;

@Override
public void onReceive(Context context, Intent intent) {
    Log.d("Inside broadcast reciever ", "Inside onrecieve");
    // TODO Auto-generated method stub
    if (intent.hasExtra("state")){
         Log.d("Inside broadcast reciever ", "hasstate");
           if (headsetConnected && intent.getIntExtra("state", 0) == 0){
            headsetConnected = false;
            Log.d("Inside broadcast reciever ", "disconnected");
           Toast.makeText(context, "Headset is disconnected", Toast.LENGTH_SHORT).show();

           }
           else if (!headsetConnected && intent.getIntExtra("state", 0) == 1){
               Log.d("Inside broadcast reciever ", "disconnected");
            headsetConnected = true;
            Intent intent2=new Intent(context, ActivityB.class);
            context.startActivity(intent2);

           }
    }
}

}`

最后我的 ACTIVITY B

public class ActivityB extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Toast.makeText(getApplicationContext(), "Headset is connected", Toast.LENGTH_SHORT).show();
    HeadsettestActivity a = HeadsettestActivity.activity ;
    a.unregister();


}

}

代码实际上有效。

答案 2 :(得分:1)

由于您没有关闭我将意图发送到活动A的其他活动,

活动B

boolean unregister = true;
Intent intent = new Intent(this, Activity_A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("unregister",unregister);
startActivity(intent);

在活动A上覆盖onNewIntent

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);

    if (intent != null) {
        if (intent.getBooleanExtra("unregister", false)) {
            unregister();

        }

    }

}