Search code examples
javaandroidbroadcastreceiver

How to send a SMS message with progress on android?


I am planning on sending SMS messages in my app. I have this code so far:

private void sendMessage(String number, String message ){

        dlg.setCancelable(false);
        dlg.setMessage("Sending...");


        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(etText.getWindowToken(), 0);

        etText.setText("");

        dlg.show();

        SmsManager sms = SmsManager.getDefault();

        Intent sendingIntent = new Intent(Intent.ACTION_SEND);
        sendingIntent.putExtra("number", number);
        sendingIntent.putExtra("message", message);
        PendingIntent sendPI = PendingIntent.getBroadcast(this, 0, sendingIntent, PendingIntent.FLAG_ONE_SHOT);

        sms.sendTextMessage(number, null, message, sendPI, null);

    }

My Receiver:

public class SMSSenderReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        int resultCode = this.getResultCode();

         String number = intent.getExtras().getString("number");
         String message = intent.getExtras().getString("message");

         ContentValues val = new ContentValues();
         val.put("address", number);
         val.put("body", message);

         switch(resultCode){

         case Activity.RESULT_OK:

                context.getContentResolver().insert(Uri.parse("content://sms/sent"), val);

                if (MessageListActivity.dlg != null){
                    if (MessageListActivity.dlg.isShowing()){
                        MessageListActivity.dlg.dismiss();
                    }
                }

         }
    }

}

In my methods for my activity:

SMSSenderReceiver receiver = new SMSSenderReceiver();

@Override
public void onCreate(Bundle b){

    this.registerReceiver(receiver,  new IntentFilter(Intent.ACTION_SEND));
}

@Override
public void onResume(){ 
  super.onResume();
   this.registerReceiver(receiver);

}

@Override
public void onStop(){
super.onStop();
this.unRegisterReceiver(receiver);
}

@Override
public void onPause(){
super.onPause();
this.unRegisterReceiver(receiver);
}

Now the problem is that when I send a message with my phone screen on, it does fine by dismissing the dialog and putting the message into the sent box when it is sent, but when I try to send a message and immediately turn off my screen it sends the message, but doesn't dismiss the dialog nor put the message into the sent folder. I know this has something to do with life cycles of the activity, but I'm not sure what to do with the onPause and onResume functions. If I don't unregister the receiver when the phone turns off then I get an error that the receiver has already been leaked error. Is there anyone that knows of a way of receiving the broadcast when my phone is off? Or of a way for getting the ACTION_SEND broadcast through the manifest?


Solution

  • Turning off screen will always call onStop(), but not with onDestroy(). onDestroy() could be called in case the system is losing memory… I think you can unregister the receiver in onDestroy()