Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Open SMS Intent

In my Android Aplication I just need to open SMS intent with pre populated message_body and the PhoneNumber.

Following is the code I am trying

Uri uri = Uri.parse(String.format("smsto:%s", strPhoneNumber));
Intent smsIntent = new Intent(Intent.ACTION_SENDTO, uri);
smsIntent.putExtra("sms_body", "Sample Body");
startActivityForResult(smsIntent, OPEN_SMS_APP);

All works great in default scenario but if Facebook Messenger is installed and setup it as the default SMS Application (settings -> Apps & Notifications -> Default Apps -> SMS app) then the functionality breaks.

Problem is, it opens FB messenger without the message_body (empty) even though it correctly picks the phone number (in FB Messenger APP).

Further, I tried following tests but didn't pick SMS_BODY or opened default Android APP

smsIntent.addCategory(Intent.CATEGORY_APP_MESSAGING); // STILL DIDN'T FIX
smsIntent.putExtra(Intent.EXTRA_TEXT, "Sample Body"); // STILL DIDN'T FIX

Questions

  1. Is there a way that I can force to open default Android SMS Application (Messages APP) even if someone have setup any other 3rd party SMS application as default App?
  2. OR Any other way I can pass message_body parameter to work in other 3rd party applications as well?
like image 426
JibW Avatar asked Mar 16 '18 13:03

JibW


1 Answers

If you really want to restrict to Google Android SMS App then can specify the package name of the Google App. But there are so many other Applications that will read 'sms_body' Intent Key.

Uri uri = Uri.parse(String.format("smsto:%s", strPhoneNumber));
Intent smsIntent = new Intent(Intent.ACTION_SENDTO, uri);
smsIntent.putExtra("sms_body", "Sample Body");
// To Force Google Android SMS APP To Open
smsIntent.setPackage("com.google.android.apps.messaging"); 
if (smsIntent.resolveActivity(getPackageManager()) != null)
{
    startActivityForResult(smsIntent, OPEN_SMS_APP);
}
else
{
    // Display error message to say Google Android SMS APP required.
}

But I would use the Application Chooser rather than just restricting to Google SMS App. Obviously user have to go through an additional step (click), but it lists all SMS Apps (every time) and user have the option to select a correctly working SMS Application.

startActivityForResult(Intent.createChooser(smsIntent, "Sample Title"), OPEN_SMS_APP); 
like image 106
Janaka H Avatar answered Oct 29 '22 15:10

Janaka H