You can Send a SMS In Background like this way :
Here i have use on button click you can send sms in background no screen appear in front of user.
(Note : Return If applicable otherwise return empty value.)
Get Phone Number of owner :
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String number = tMgr.getLine1Number();
write Pending Intent
this code in on click event.
String message = "HI THIS IS TEST SMS IN ANDROID.";
/** Creating a pending intent which will be broadcasted when an sms message is successfully sent */
PendingIntent piSent = PendingIntent.getBroadcast(getBaseContext(), 0, new Intent("sent_msg") , 0);
/** Creating a pending intent which will be broadcasted when an sms message is successfully delivered */
PendingIntent piDelivered = PendingIntent.getBroadcast(getBaseContext(), 0, new Intent("delivered_msg"), 0);
/** Getting an instance of SmsManager to sent sms message from the application*/
SmsManager smsManager = SmsManager.getDefault();
/** Sending the Sms message to the intended party */
smsManager.sendTextMessage(number, null, message, piSent, piDelivered);
Create class name with SmsNotifications
which extends BroadcastReceiver
/**
* This class handles the SMS sent and sms delivery broadcast intents
*/
public class SmsNotifications extends BroadcastReceiver{
/**
* This method will be invoked when the sms sent or sms delivery broadcast intent is received
*/
@Override
public void onReceive(Context context, Intent intent) {
/**
* Getting the intent action name to identify the broadcast intent ( whether sms sent or sms delivery )
*/
String actionName = intent.getAction();
if(actionName.equals("sent_msg")){
switch(getResultCode()){
case Activity.RESULT_OK:
Toast.makeText(context, "Message is sent successfully" , Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(context, "Error in sending Message", Toast.LENGTH_SHORT).show();
break;
}
}
if(actionName.equals("delivered_msg")){
switch(getResultCode()){
case Activity.RESULT_OK:
Toast.makeText(context, "Message is delivered" , Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(context, "Error in the delivery of message", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
Manage your Manifest File :
Permission :
<uses-permission android:name="android.permission.SEND_SMS" />
And
<receiver android:name=".SmsNotifications" >
<intent-filter >
<action android:name="sent_msg" />
<action android:name="delivered_msg" />
</intent-filter>
</receiver>