简体   繁体   中英

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?

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); 

it opens FB messenger without the message_body (empty)

There is no requirement for any app to honor undocumented Intent extras.

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

EXTRA_TEXT is not documented for use with ACTION_SENDTO , nor is CATEGORY_APP_MESSAGING .

Is there a way that I can force to open default Android SMS Application (Messages APP)

There are ~10,000 Android device models. I would expect there to be dozens, if not hundreds, of pre-installed "Android SMS Application". None of them have to honor Intent extras or categories not specifically documented to be supported by ACTION_SENDTO .

Any other way I can pass message_body parameter to work in other 3rd party applications as well?

ACTION_SEND — not ACTION_SENDTO — offers EXTRA_TEXT . However, you cannot mandate where the content should be sent. That would be up to the user.

You can also use SmsManager to send the SMS directly, if you hold the SEND_SMS permission.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM