Sending SMS programmatically without opening message app
You can send messages from your application through this:
public void sendSMS(String phoneNo, String msg) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, msg, null, null);
Toast.makeText(getApplicationContext(), "Message Sent",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(),ex.getMessage().toString(),
Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
}
Also, you need to give SEND_SMS
permission in AndroidManifest.xml
to send a message
<uses-permission android:name="android.permission.SEND_SMS" />
Yes, found the answer to my own question :)
Use the following code for the same :
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(srcNumber, null, message, null, null);
This requires the following permission to be declared on the android manifest xml.
<uses-permission android:name="android.permission.SEND_SMS"/>
Sending sms with permission request :
Add In manifest :
<uses-permission android:name="android.permission.SEND_SMS" />
Add Java Function :
void sendSmsMsgFnc(String mblNumVar, String smsMsgVar)
{
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED)
{
try
{
SmsManager smsMgrVar = SmsManager.getDefault();
smsMgrVar.sendTextMessage(mblNumVar, null, smsMsgVar, null, null);
Toast.makeText(getApplicationContext(), "Message Sent",
Toast.LENGTH_LONG).show();
}
catch (Exception ErrVar)
{
Toast.makeText(getApplicationContext(),ErrVar.getMessage().toString(),
Toast.LENGTH_LONG).show();
ErrVar.printStackTrace();
}
}
else
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
requestPermissions(new String[]{Manifest.permission.SEND_SMS}, 10);
}
}
}
public void sendLongSMS() {
String phoneNumber = "0123456789";
String message = "Hello World! Now we are going to demonstrate " +
"how to send a message with more than 160 characters from your Android application.";
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(message);
smsManager.sendMultipartTextMessage(phoneNumber, null, parts, null, null);
}
and don't forget to add
<uses-permission android:name="android.permission.SEND_SMS"/>