2012-03-20 13 views
41

Estoy tratando de enviar sms amd mail juntos. No hay problemas con el envío de correo, pero cuando envío de SMS que reciben esta Excepción:Se ha filtrado la actividad IntentReceiver

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

Aquí está mi código para el método SMS:

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

} 

} 

Respuesta

29

Crear un receptor de encargo como éste

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

     } 
} 

y un receptor enviado como esto ..

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


     } 

ahora en el método sendSMS después de este

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

poner

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

Ahora anular onPause y anular el registro de los receptores de este tipo ..

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

intente de esta manera y dígame si esto funcionó .. – 5hssba

+0

funcionó bien ...... ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡compañero !! – chetan

+0

simplemente realice los siguientes cambios: use registerReceiver (sendBroadcastReceiver, new IntentFilter (SENT)) y registerReceiver (deliveryBroadcastReciever, new IntentFilter (ENTREGADO)); – chetan

11

Debe anular el registro de los receptores en onPause() y registrarlos en onResume(). De esta forma, cuando Android destruya y vuelva a crear la actividad para el cambio de configuración, o por alguna razón, todavía tendrá receptores configurados.

+0

oye u puede decirme cómo hacer u ... con respecto al programa anterior !! – chetan

+1

¿Por qué registrarlos en onResume() vs. onCreate()? –

+1

@IgorGanapolsky porque se llamará a 'onResume' y' onPause' cada vez que se muestre y oculte la interfaz de usuario, respectivamente. No quieres escuchar transmisiones de eso. – iceman

Cuestiones relacionadas