本文介绍了活动已泄漏IntentReceiver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想发送短信AMD邮件一起....与发送邮件钻时,我发短信没有问题,我收到此异常:结束渗漏IntentReceiver是否缺少调用unregisterReceiver(?)。你们只要告诉我在这里需要更改注销reciever?

这是我的code短信方式:

 公共类端延伸活动{

    按钮btnSendSMS;
    的EditText txtPhoneNo;
    的EditText txtMessage;
    公众的EditText详细信息;
    公共字符串的用户;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.end);
        详情=(EditText上)findViewById(R.id.details);
        btnSendSMS =(按钮)findViewById(R.id.btnSend);
        叠B = this.getIntent()getExtras()。
        最后弦乐电子邮件= b.getString(keym);
        最后弦乐PNO = b.getString(钥匙);

        btnSendSMS.setOnClickListener(新View.OnClickListener(){
            公共无效的onClick(视图v){
                。字符串细节= Details.getText()的toString();
                邮件M =新邮件([email protected],sdfsa);
                的String [] toArr = {电子邮件};
                m.setTo(toArr);
                m.setFrom([email protected]);
                m.setSubject(紧急情况);
                m.setBody(详细);

                尝试 {
                    // m.addAttachment(/ SD卡/ filelocation);
                    如果(m.send()){
                    Toast.makeText(End.this,邮件发送成功,Toast.LENGTH_LONG).show();
                    } 其他 {
                  Toast.makeText(End.this,电子邮件未发送,Toast.LENGTH_LONG).show();
                    }
                }赶上(例外五){
                //Toast.makeText(MailApp.this,有一个问题,发送电子邮件。Toast.LENGTH_LONG).show();
                Log.e(MailApp,无法发送电子邮件,E);
                }

                sendSMS(PNO,细节);
                完();
                意向意图=新的意图(End.this,Service.class);
                startActivity(意向);
               }
            }
        );

    }

    私人无效sendSMS(字符串phoneNumber的,字符串消息)
    {
         字符串SENT =SMS_SENT;
         字符串DELIVERED =SMS_DELIVERED;

         PendingIntent sentPI = PendingIntent.getBroadcast(此,0,
             新的意图(SENT),0);

         PendingIntent deliveredPI = PendingIntent.getBroadcast(此,0,
             新的意图(交付),0);

         // ---当短信已发送---
         registerReceiver(新BroadcastReceiver的()
         {
             上下文语境;
             @覆盖
             公共无效的onReceive(背景为arg0,意图ARG1){
                 开关(的getResult code())
                 {
                     案例Activity.RESULT_OK:
                         Toast.makeText(getBaseContext(),短信发送
                                 Toast.LENGTH_SHORT).show();
                         打破;
                     案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                         Toast.makeText(getBaseContext(),一般故障,
                                 Toast.LENGTH_SHORT).show();
                         打破;
                     案例SmsManager.RESULT_ERROR_NO_SERVICE:
                         Toast.makeText(getBaseContext(),无服务,
                                 Toast.LENGTH_SHORT).show();
                         打破;
                     案例SmsManager.RESULT_ERROR_NULL_PDU:
                         Toast.makeText(getBaseContext(),空的PDU,
                                 Toast.LENGTH_SHORT).show();
                         打破;
                     案例SmsManager.RESULT_ERROR_RADIO_OFF:
                         Toast.makeText(getBaseContext(),无线电关
                                 Toast.LENGTH_SHORT).show();
                         打破;
                 }


             }
         },新的IntentFilter(发送));

         // ---当SMS已交付---
         registerReceiver(新BroadcastReceiver的(){
             上下文语境;
             @覆盖

             公共无效的onReceive(背景为arg0,意图ARG1)
             {
                 开关(的getResult code())
                 {
                     案例Activity.RESULT_OK:
                         Toast.makeText(getBaseContext(),SMS递送,
                                 Toast.LENGTH_SHORT).show();
                         打破;
                     案例Activity.RESULT_CANCELED:
                         Toast.makeText(getBaseContext(),短信未交付,
                                 Toast.LENGTH_SHORT).show();
                         打破;
                 }

             }
         },新的IntentFilter(交付));

         SmsManager的短信= SmsManager.getDefault();
         sms.sendTextMessage(phoneNumber的,空,消息,sentPI,deliveredPI);

}

}

 

解决方案

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

 类deliverReceiver扩展的BroadcastReceiver {
@覆盖
 公共无效的onReceive(上下文的背景下,意图ARG1){
             开关(的getResult code())
             {
                 案例Activity.RESULT_OK:
                     Toast.makeText(getBaseContext(),SMS递送,
                             Toast.LENGTH_SHORT).show();
                     打破;
                 案例Activity.RESULT_CANCELED:
                     Toast.makeText(getBaseContext(),短信未交付,
                             Toast.LENGTH_SHORT).show();
                     打破;
             }

         }
}
 

和这样的发送reciever ..

 类sentReceiver扩展的BroadcastReceiver {
@覆盖
 公共无效的onReceive(上下文的背景下,意图ARG1){
             开关(的getResult code())
             {
                 案例Activity.RESULT_OK:
                     Toast.makeText(getBaseContext(),短信发送
                             Toast.LENGTH_SHORT).show();
                     打破;
                 案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                     Toast.makeText(getBaseContext(),一般故障,
                             Toast.LENGTH_SHORT).show();
                     打破;
                 案例SmsManager.RESULT_ERROR_NO_SERVICE:
                     Toast.makeText(getBaseContext(),无服务,
                             Toast.LENGTH_SHORT).show();
                     打破;
                 案例SmsManager.RESULT_ERROR_NULL_PDU:
                     Toast.makeText(getBaseContext(),空的PDU,
                             Toast.LENGTH_SHORT).show();
                     打破;
                 案例SmsManager.RESULT_ERROR_RADIO_OFF:
                     Toast.makeText(getBaseContext(),无线电关
                             Toast.LENGTH_SHORT).show();
                     打破;
             }


         }
 

现在在sendSMS后,该方法

  PendingIntent deliveredPI = PendingIntent.getBroadcast(此,0,
         新的意图(交付),0);
 

  registerReceiver(sentReceiver,发送);
registerReceiver(deliverReceiver,送到);
 

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

  unregisterReceiver(sentReceiver);
unregisterReceiver(deliverReceiver);
 

I am trying to send sms amd mail together....no issues with sending mail bur when i send sms i receive this Exception: End has leaked IntentReceiver Are you missing a call to unregisterReceiver() ?. guys just tell me changes required here to unregister reciever ?

here is my code for sms method:

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

}

}

解决方案

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

这篇关于活动已泄漏IntentReceiver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 02:16