簡體   English   中英

取消注冊廣播接收器錯誤

[英]Unregistering Broadcast Receiver error

我想在我的 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注冊接收器的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);
    }
}

關於未注冊的接收器,我有什么遺漏嗎?

您在onDestroy()onStop()中注銷了兩次。 如果您在 onCreate 方法中注冊廣播,則必須在onDestroy取消注冊;如果在onStart中注冊,則必須在onStop取消注冊。 在您的情況下,您正在 clickListener 中注冊廣播,因此如果用戶沒有單擊此按鈕,則廣播未注冊,您將在此之后嘗試取消注冊。 所以如果廣播被注冊,你必須做一個測試。 我建議你添加一個布爾屬性,當用戶點擊這個按鈕時,你在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