取消注册广播接收器错误

时间:2016-03-10 08:38:06

标签: java android bluetooth broadcastreceiver android-broadcastreceiver

我想在我的Android应用程序中获取可用的蓝牙设备,以便我可以通过我的应用程序配对或不公平设备。我正在注册广播接收器以通知我的新设备列表。我在一个活动中执行此操作,但是当我使用finish()销毁我的活动时,即使我取消注册接收器,它仍然会给我以下错误。

例外:

03-10 10:24:48.790 25219-25219/com.vidame E/ActivityThread: Activity com.vidame.Activities.BloodPressureActivity has leaked IntentReceiver com.vidame.HelperClasses.Bluetooth$mPairReceiver@37341ff5 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity com.vidame.Activities.BloodPressureActivity has leaked IntentReceiver com.vidame.HelperClasses.Bluetooth$mPairReceiver@37341ff5 that was originally registered here. Are you missing a call to unregisterReceiver()?
    at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:904)
    at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:705)
    at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1685)
    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1665)
    at android.app.ContextImpl.registerReceiver(ContextImpl.java:1659)
    at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:495)
    at com.vidame.HelperClasses.Bluetooth.registerBluetoothBroadcast(Bluetooth.java:83)
    at com.vidame.Activities.BloodPressureActivity$2.onClick(BloodPressureActivity.java:119)
    at android.view.View.performClick(View.java:4789)
    at android.view.View$PerformClick.run(View.java:19881)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5292)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

我在 Bluetooth.java 类中使用了我的broadcast receiver方法,并在我的应用中使用了其他有用的方法,并在BloodPressureActivity中调用它。下面我有这两个类的代码

这是我在BloodPressureActivity

中注册接收器的地方
devices_list.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            bluetooth.registerBluetoothBroadcast(BloodPressureActivity.this, mAdapter, list, mProgressDlg, mListView);
            device_find.setVisibility(View.GONE);
            devices_list.setVisibility(View.GONE);
            list_view.setVisibility(View.VISIBLE);
            bluetooth.bAdapter().startDiscovery();
        }
    });
  

我在我的onDestroy()和onStop()

中取消注册
@Override
public void onDestroy() {
    bluetooth.unRegisterReceiver();
    super.onDestroy();
}

@Override
protected void onStop() {
    bluetooth.unRegisterReceiver();
    super.onStop();
}

现在在Bluetooth.class我的广播接收器

 public void registerBluetoothBroadcast (Activity parent, DeviceListAdapter mAdapter, ArrayList<BluetoothDevice> list, ProgressDialog mProgressDlg, ListView mListView){
    this.activity=parent;
    this.mAdapter=mAdapter;
    this.list=list;
    this.mProgressDlg=mProgressDlg;
    this.mListView=mListView;

    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    filter.addAction(BluetoothDevice.ACTION_FOUND);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    //filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
    activity.registerReceiver(mPairReceiver, filter);
}


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

        if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
            final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
            final int prevState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothDevice.ERROR);

            if (state == BluetoothDevice.BOND_BONDED && prevState == BluetoothDevice.BOND_BONDING) {
                ShowMessage("Paired");
            } else if (state == BluetoothDevice.BOND_NONE && prevState == BluetoothDevice.BOND_BONDED){
                ShowMessage("Unpaired");
            }
            mAdapter.notifyDataSetChanged();
        }else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
            list = new ArrayList<>();
            mProgressDlg.show();
        } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
            mProgressDlg.dismiss();
            showDevicesList(list);
        } else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            list.add(device);
            ShowMessage("Found device " + device.getName());
        }
    }
};

public void unRegisterReceiver(){
    if(mPairReceiver!=null){
        LocalBroadcastManager.getInstance(activity).unregisterReceiver(mPairReceiver);
    }
}

我是否遗漏了未注册接收者的事情?

1 个答案:

答案 0 :(得分:1)

您在onDestroy()onStop()中取消注册两次。 如果您在onCreate方法中注册广播,则必须在onDestroy中取消注册,如果您在onStart注册广告,则必须在onStop中取消注册。 在您的情况下,您在clickListener中注册广播,因此如果用户未单击此按钮,则不会注册广播,并且您之后尝试取消注册。因此,如果广播已注册,您必须进行测试。 我建议你添加一个布尔属性,当用户点击这个按钮时,如果你在true你在这个布尔值上测试,你将这个属性设置为onDestroy,如果它是真的,你取消注册你的广播。 / p>

devices_list.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            bluetooth.registerBluetoothBroadcast(BloodPressureActivity.this, mAdapter, list, mProgressDlg, mListView);
            registred = true;
            device_find.setVisibility(View.GONE);
            devices_list.setVisibility(View.GONE);
            list_view.setVisibility(View.VISIBLE);
            bluetooth.bAdapter().startDiscovery();
        }
    });

onDestroy{
...//your code
if(registred){
unregisterBroadcast(your broadcast);
}

}