android - Activity 泄露了 IntentReceiver

标签 android broadcastreceiver

我正在尝试一起发送短信和邮件。发送邮件没有问题,但是当我发送短信时收到此异常:

End has leaked IntentReceiver
Are you missing a call to unregisterReceiver()? 

这是我的短信方法代码,来自 https://mobiforge.com/design-development/sms-messaging-android :

public class End extends Activity {
    
    Button btnSendSMS;
    EditText txtPhoneNo;
    EditText txtMessage;
    public EditText Details;
    public String user;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.end);
        Details = (EditText)findViewById(R.id.details);
        btnSendSMS = (Button) findViewById(R.id.btnSend);
        Bundle b=this.getIntent().getExtras();
        final String email=b.getString("keym");
        final String pno=b.getString("keys");

        btnSendSMS.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {           
                String detail=Details.getText().toString();
                Mail m = new Mail("abc@gmail.com", "sdfsa"); 
                String[] toArr = {email}; 
                m.setTo(toArr); 
                m.setFrom("asdasd11@gmail.com"); 
                m.setSubject("EMERGENCY");
                m.setBody(detail);
             
                try { 
                    // m.addAttachment("/sdcard/filelocation"); 
                    if(m.send()) { 
                    Toast.makeText(End.this, "Email was sent successfully.", Toast.LENGTH_LONG).show(); 
                    } else { 
                  Toast.makeText(End.this, "Email was not sent.", Toast.LENGTH_LONG).show(); 
                    } 
                } catch(Exception e) { 
                //Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show(); 
                Log.e("MailApp", "Could not send email", e); 
                } 
              
                sendSMS(pno, detail);
                finish();
                Intent intent = new Intent(End.this,Service.class);
                startActivity(intent);
               }   
            }
        );        
    
    } 

    private void sendSMS(String phoneNumber, String message)
    {        
         String SENT = "SMS_SENT";
         String DELIVERED = "SMS_DELIVERED";
  
         PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
             new Intent(SENT), 0);
  
         PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
             new Intent(DELIVERED), 0);
  
         //---when the SMS has been sent---
         registerReceiver(new BroadcastReceiver()
         {
             Context context;
             @Override
             public void onReceive(Context arg0, 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;
                 }
               

             }
         }, new IntentFilter(SENT));
  
         //---when the SMS has been delivered---
         registerReceiver(new BroadcastReceiver(){
             Context context;
             @Override
            
             public void onReceive(Context arg0, 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;                        
                 }
             
             }
         }, new IntentFilter(DELIVERED));        
  
         SmsManager sms = SmsManager.getDefault();
         sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);    
    
}
    
}

最佳答案

像这样创建一个自定义接收器

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

         }
}

和这样的发送接收者..

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


         }

现在在 sendSMS 方法之后

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

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

现在覆盖 onpause 并取消注册这样的接收器..

unregisterReceiver(sentReceiver);
unregisterReceiver(deliverReceiver);

关于android - Activity 泄露了 IntentReceiver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9781769/

相关文章:

android 包重新启动广播不起作用

android - 在textview android上捏缩放

android - 根据 map View 中的最小/最大纬度/经度计算 zoomToSpan() 度数

android - 使用 DataBase iOS 和 Android 发送应用程序

java - Android:AsyncTask 内的 Java Net Socket 异常

android - DialogFragment 缺少内容,只显示 "ok"按钮

Android短信广播接收器,阅读短信

java - 为什么广播接收器不能动态工作?

android - 检测传入的 SMS 消息

android - Broadcastreceiver 创建多个 TelephonyManager 实例