Activity has leaked IntentReceiver

前端 未结 3 2053
无人共我
无人共我 2020-12-13 23:37

I am trying to send sms amd mail together. There are no issues with sending mail, but when i send sms I receive this Exception:

End has leaked IntentReceiver         


        
相关标签:
3条回答
  • 2020-12-14 00:16

    You should unregister the receivers in onPause() and register them in onResume(). This way, when Android destroys and recreates the activity for the configuration change, or for any reason you will still have receivers set up.

    0 讨论(0)
  • 2020-12-14 00:40

    Create a custom receiver like this

    class deliverReceiver extends BroadcastReceiver {     
    @Override
     public void onReceive(Context context, Intent arg1) {
                 switch (getResultCode())
                 {
                     case Activity.RESULT_OK:
                         Toast.makeText(getBaseContext(), "SMS delivered", 
                                 Toast.LENGTH_SHORT).show();
                         break;
                     case Activity.RESULT_CANCELED:
                         Toast.makeText(getBaseContext(), "SMS not delivered", 
                                 Toast.LENGTH_SHORT).show();
                         break;                        
                 }
    
             }
    }
    

    and a sent reciever like this..

    class sentReceiver extends BroadcastReceiver {     
    @Override
     public void onReceive(Context context, Intent arg1) {
                 switch (getResultCode())
                 {
                     case Activity.RESULT_OK:
                         Toast.makeText(getBaseContext(), "SMS sent", 
                                 Toast.LENGTH_SHORT).show();
                         break;
                     case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                         Toast.makeText(getBaseContext(), "Generic failure", 
                                 Toast.LENGTH_SHORT).show();
                         break;
                     case SmsManager.RESULT_ERROR_NO_SERVICE:
                         Toast.makeText(getBaseContext(), "No service", 
                                 Toast.LENGTH_SHORT).show();
                         break;
                     case SmsManager.RESULT_ERROR_NULL_PDU:
                         Toast.makeText(getBaseContext(), "Null PDU", 
                                 Toast.LENGTH_SHORT).show();
                         break;
                     case SmsManager.RESULT_ERROR_RADIO_OFF:
                         Toast.makeText(getBaseContext(), "Radio off", 
                                 Toast.LENGTH_SHORT).show();
                         break;
                 }
    
    
             }
    

    now in sendSMS method after this

     PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
             new Intent(DELIVERED), 0);
    

    put

    registerReceiver(sentReceiver,SENT);
    registerReceiver(deliverReceiver,DELIVERED);
    

    Now override onpause and unregister for the receivers like this..

    unregisterReceiver(sentReceiver);
    unregisterReceiver(deliverReceiver);
    
    0 讨论(0)
  • 2020-12-14 00:41

    You have registered two broadcast receivers in your activity. In onDestroy of your activity, unregister both the receivers. Refer http://developer.android.com/reference/android/content/ContextWrapper.html#unregisterReceiver(android.content.BroadcastReceiver).

    0 讨论(0)
提交回复
热议问题