I'm facing a weird problem on Android. I want to listen for a delivery intent when I send a text message through my app. Here is what I've done so far:
private void shootSMS(String number,long currentTime){
SmsManager smsManager = SmsManager.getDefault();
Intent sentIntent = new Intent(SentBroadcastReceiver.SENT_ACTION);
Intent deliveredIntent = new Intent(DeliveredBroadcastReceiver.DELIVERED_ACTION);
Bundle b = new Bundle();
b.putString("num",number);
b.putString("record_time",currentTime+"");
sentIntent.putExtras(b);
deliveredIntent.putExtras(b);
PendingIntent sentPendingIntent = PendingIntent.getBroadcast(this,(int)currentTime,sentIntent,PendingIntent.FLAG_ONE_SHOT);
PendingIntent deliveredPendingIntent = PendingIntent.getBroadcast(this,(int)currentTime,deliveredIntent,PendingIntent.FLAG_ONE_SHOT);
smsManager.sendTextMessage(number,null,getString(R.string.app_name),sentPendingIntent,deliveredPendingIntent);
}
Now the problem is that when the onReceive()
methods of my broadcast receivers are called I call the getResultCode()
method, and it always return -1! Even if the phone is turned off, so it makes it impossible to track whether the SMS is delivered or not!
I checked the number with the GoSMSPro and sent a SMS which failed. The interesting thing is that when I put my phone in Airplane Mode I get a Result Code equal to 2
which is SmsManager.RESULT_ERROR_RADIO_OFF
Now the question is what's wrong in here?
The documentation on sendTextMessage states:
If you go look at Activity.RESULT_OK the value is -1.
So the resultCode of -1 is actually RESULT_OK.
In general it is best not to look at numeric values but to try to figure out which constant they are representing.
do like this:
and your Receiver:
note that use "Activity.RESULT_OK" instead number
permission:
this permission use for sending and receiving sms.
Try This approach : To send a text message use sendTextMessage from SMS Manager.
The first Pending Intent parameter (sentIntent) is fired when the message is either successfully sent or failed. The result code for the Broadcast receiever that receives this Intent will be one of the following.
The second Pending parameter(deliveryIntent) is fired only after the recipient receives your SMS message.
After sent and delivery events display appropriate messages through Toast.
activity_main.xml
MainActivity.java
Hope this helped !