Receiver not registered exception error?

后端 未结 10 2249
醉酒成梦
醉酒成梦 2020-11-28 05:41

In my developer console people keep reporting an error that I cannot reproduce on any phone I have. One person left a message saying he gets it when they try to open the set

10条回答
  •  醉酒成梦
    2020-11-28 06:09

    Declare receiver as null and then Put register and unregister methods in onResume() and onPause() of the activity respectively.

    @Override
    protected void onResume() {
        super.onResume();
        if (receiver == null) {
            filter = new IntentFilter(ResponseReceiver.ACTION_RESP);
            filter.addCategory(Intent.CATEGORY_DEFAULT);
            receiver = new ResponseReceiver();
            registerReceiver(receiver, filter);
        }
    }      
    
    @Override
    protected void onPause() {
        super.onPause();
        if (receiver != null) {
            unregisterReceiver(receiver);
            receiver = null;
        }
    }
    

提交回复
热议问题