i use the code below to send sms and it works fine, when the phone is connected to mobile network. The message is shown in the default sms app of the phone, all fine.
But when there is no network coverage, its not behaving very well. The message is shown in the default sms app with the indication : "Not sent. Tap to try again.". And it will not send it automatically when the phone gets back in network coverage.
The desirable behavior would be to send it automatically, when the phone is back in network. Like the default sms app, which when used to send a message while offline, it says:"Not sent. Trying again.." and the message will be send automatically when online again.
Here is the code i use:
void sendMessage(String phoneNumber, String message) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
Toast.makeText(activity, "SMS sent!", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(activity, "SMS sending failed: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
i also tried this without success void sendMessage(String phoneNumber, String message) { Intent intent=new Intent(activity.getApplicationContext(),MainCustomAdapter.class); PendingIntent pi=PendingIntent.getActivity(activity.getApplicationContext(), 0, intent,PendingIntent.FLAG_IMMUTABLE); try { SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(phoneNumber, null, message, pi, null); Toast.makeText(activity, "SMS sent!", Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(activity, "SMS sending failed: " + e.getMessage(), Toast.LENGTH_SHORT).show(); } }