Unable to start default sms action in application stack. The problem observed in 6 lollipops on a lollipop and android, 1 marshmallow.
I tried to send the intent ACTION_VIEW and set smsto: and sms body.
I can run the sms application.
I have the following behavior of my application.
I have a list of contact numbers in my application
When the user says the invitation, I want to start the default sms application, fill it in and send sms, and the default sms application should be in my activity task.
By pushing back I want to close the messaging app.
Please see the reference images below 1. Invitation to the application 2. invite, launch sms
send SMS, press Back, check the list of recent applications
What I can achieve. I can achieve the above in kitkat. (by default the sms application ends) but not in candy and marshmallows. By default, the sms application goes into the background.
- Launch the default sms application and fill in the data.
- When you tap back, the messaging application goes into the background.
- Now I can start the sms application from the recent list of applications, which again populates the recipient with the same number and body that is not intended.
The problem is that the application starts in a new task. I cannot get this messaging app in my activity task. If I return to this messaging app, I can return to my app. But if I select a messaging application from a recent list, it shows every time the sms recipient and body fill in activity.
Please find the code snippet below.
private void sendTextMessage(String to) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // At least KitKat { String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(mActivity); // Need to change the build to API 19 Logger.log_error(TAG + "sendTextMessage() above KITKAT"); Intent sendIntent = new Intent(Intent.ACTION_SEND); sendIntent.setType("text/plain"); sendIntent.putExtra("address", to); sendIntent.putExtra("sms_body", "sms body"); sendIntent.putExtra(Intent.EXTRA_TEXT, "sms body"); if (defaultSmsPackageName != null)// Can be null in case that there is no default, then the user would be able to choose // any app that support this intent. { sendIntent.setPackage(defaultSmsPackageName); Logger.log_error(TAG + "sendTextMessage() defaultSmsPackageName = " + defaultSmsPackageName); } getContext().startActivity(sendIntent); } else // For early versions, do what worked for you before. { Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW); Logger.log_error(TAG + "sendTextMessage() below KITKAT"); smsIntent.setType("vnd.android-dir/mms-sms"); smsIntent.putExtra("address", to); smsIntent.putExtra("sms_body", getString(R.string.str_share_app_short_text)); startActivity(smsIntent); } }
This startup method messes with the messaging app.
I tried to run the intent without history, one instance, excluded from recents.
For reference, I am attaching screenshots of WhatsApp, which I am trying to use in my work. I cannot post more than two links in a stack overflow, so I only keep a list of recent applications.
- Launch whatsapp, go to contacts, scroll down, find the invitation in green and invite.
- The sms application will start.
- Check the shipment.
check last list