0

我在将我的接收器从另一个活动中注销时遇到了麻烦。这里是:

在我的启动活动 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。在此之后,我进入 Activity B 的 onCreate() 方法,做我的事情,在 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 为空,看起来它没有得到它的引用。此外,Receiver 不会取消注册,并且仍在收听。我很确定我的错误在于引用/调用;但我不知道如何正确地做:/

我在这里想念什么?有人能帮我吗?

4

3 回答 3

1

通过 intent.putExtra() 方法将您的 Activity A 上下文传递给 Activity B。然后代替做 MainActivity m = new MainActivity();do MainActivity m = ((MainActivity)(passedContext));

于 2013-09-02T13:10:38.553 回答
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);

在 Activity A 上,您覆盖 onNewIntent

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

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

        }

    }

}
于 2013-09-02T14:48:17.097 回答
1

我已经成功地尝试过这样的事情,看看这是否有帮助:我有一个 HeadSetTestTActivity ,一个 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
`公共类 Headsetconnectorreciever 扩展 BroadcastReceiver { 私有布尔耳机连接 = 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);

           }
    }
}

}`

最后是我的活动 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();


}

}

代码确实有效。

于 2013-09-02T14:31:13.883 回答