简体   繁体   English

取消注册广播接收器错误

[英]Unregistering Broadcast Receiver error

I want to get the available bluetooth devices in my android app so as i can pair or unfair a device through my app.我想在我的 android 应用程序中获取可用的蓝牙设备,以便我可以通过我的应用程序配对或不公平的设备。 I am registering a broadcast receiver to inform my list of new devices.我正在注册一个广播接收器来通知我的新设备列表。 I do this in an activity, but when i destroy my activity with finish() even though i am unregistering the receiver it keeps giving me the following error.我在一个活动中这样做,但是当我使用finish()销毁我的活动时,即使我正在取消注册接收器,它也会不断给我以下错误。

Exception:例外:

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)

My have my broadcast receiver method in Bluetooth.java class with other useful methods i use in my app and call it in BloodPressureActivity .我在Bluetooth.java类中有我的broadcast receiver方法以及我在我的应用程序中使用的其他有用方法,并在BloodPressureActivity调用它。 Below i have code for both these classes下面我有这两个类的代码

This is where i register the receiver in BloodPressureActivity这是我在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();
        }
    });

and i unregister it in my onDestroy() and onStop()我在 onDestroy() 和 onStop() 中取消注册

@Override
public void onDestroy() {
    bluetooth.unRegisterReceiver();
    super.onDestroy();
}

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

Now in the Bluetooth.class my broadcast receiver现在在 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);
    }
}

Is there something the i am missing about unregistered the receiver?关于未注册的接收器,我有什么遗漏吗?

You are unregistering it twice, in your onDestroy() and in onStop() .您在onDestroy()onStop()中注销了两次。 If you are registering your broadcast in your onCreate method, you must unregister it in onDestroy and if you are registering it in onStart you must unregister it in onStop .如果您在 onCreate 方法中注册广播,则必须在onDestroy取消注册;如果在onStart中注册,则必须在onStop取消注册。 In your case, you are registering the broadcast in a clickListener, so if the user doesn't click on this button, the broadcast is not registered and you are trying to unregister it after that.在您的情况下,您正在 clickListener 中注册广播,因此如果用户没有单击此按钮,则广播未注册,您将在此之后尝试取消注册。 so you must do a test if the broadcast is registered.所以如果广播被注册,你必须做一个测试。 I advise you to add a boolean attribute and when the user click on this button you set this attribute to true once you are in onDestroy you test on this boolean if it is true you unregister your broadcast.我建议你添加一个布尔属性,当用户点击这个按钮时,你在onDestroy将此属性设置为true ,你测试这个布尔值,如果它是真的,你取消注册你的广播。

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);
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM