2012-03-20 111 views
41

我试图发送短信和邮件在一起。有与发送邮件没有问题,但是当我发送短信我收到此异常:活动已泄露IntentReceiver

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

这里是我的手机短信的方法代码:

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("[email protected]", "sdfsa"); 
       String[] toArr = {email}; 
       m.setTo(toArr); 
       m.setFrom("[email protected]"); 
       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);  

} 

} 

回答

29

在此之后

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

registerReceiver(sentReceiver,SENT); 
registerReceiver(deliverReceiver,DELIVERED); 
像这样创建

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

     } 
} 

自定义接收器和发送reciever这样的..

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方法

现在覆盖的onPause和取消像这样的接收器..

unregisterReceiver(sentReceiver); 
unregisterReceiver(deliverReceiver); 
+0

尝试这样,让我知道这是否工作.. – 5hssba 2012-03-20 06:24:10

+0

工作得很好......欢呼队友! – chetan 2012-03-20 16:03:13

+0

只做了以下更改:使用registerReceiver(sendBroadcastReceiver,新的IntentFilter(SENT))和 registerReceiver(deliveryBroadcastReciever,new IntentFilter(DELIVERED)); – chetan 2012-03-20 16:04:39

11

您应该注销onPause()中的接收器并在onResume()中注册它们。这样,当Android销毁并重新创建配置更改的活动时,或者出于任何原因,您仍然会设置接收器。

+0

嘿u能告诉我如何ü做...相对于上述程序! – chetan 2012-03-20 06:08:31

+1

为什么要在onResume()和onCreate()中注册它们? – 2012-08-29 15:28:44

+1

@IgorGanapolsky,因为每次显示和隐藏UI时都会调用onResume和onPause。你不想听广播。 – iceman 2015-10-17 07:46:25

相关问题