Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to send a SMS message with progress on android?
    primarykey
    data
    text
    <p>I am planning on sending SMS messages in my app. I have this code so far:</p> <pre><code>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); } </code></pre> <p>My Receiver:</p> <pre><code>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(); } } } } } </code></pre> <p>In my methods for my activity:</p> <pre><code>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); } </code></pre> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload