Android KitKat 4.4 Hangouts cannot handle Sending SMS intent
I attached a code that solve the problem by doing the following:
- Check the OS version
- In case that older version (prior to KitKat), use the old method
- If new API, check the default sms package. if there is any, set it as the package, otherwise, let the user choose the sharing app.
Here is the code:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) //At least KitKat
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(activity); //Need to change the build to API 19
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, smsText);
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);
}
activity.startActivity(sendIntent);
}
else //For early versions, do what worked for you before.
{
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
sendIntent.putExtra("sms_body", smsText);
activity.startActivity(sendIntent);
}
Android4.4 can not handle sms intent with vnd.android-dir/mms-sms
To start the SMS app with number populated use action ACTION_SENDTO
:
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" + Uri.encode(phoneNumber)));
startActivity(intent);
This will work on Android 4.4. It should also work on earlier versions of Android however as the APIs were never public the behavior might vary. If you didn't have issues with your prior method I would probably just stick to that pre-4.4 and use ACTION_SENDTO
for 4.4+.
ActivityNotFoundException for sms intent
if (getDefaultSmsAppPackageName() != null) {
Uri smsUri= Uri.parse("smsto:" + phoneNumber);
Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
intent.putExtra("sms_body", "sms text");
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
}
public static String getDefaultSmsAppPackageName(@NonNull Context context) {
String defaultSmsPackageName;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context);
return defaultSmsPackageName;
} else {
Intent intent = new Intent(Intent.ACTION_VIEW)
.addCategory(Intent.CATEGORY_DEFAULT).setType("vnd.android-dir/mms-sms");
final List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(intent, 0);
if (resolveInfos != null && !resolveInfos.isEmpty())
return resolveInfos.get(0).activityInfo.packageName;
}
return null;
}
Enabling SMS support in Hangouts 2.0 breaks the BroadcastReceiver of SMS_RECEIVED in my app
Fixed it.
The first problem was that, as you can see in revision 2 of my question, I put the priority
attribute within the action
element, when it actually belongs into the intent-filter
element. So the priority didn't work.
While still targeting API 19, I did some experiences with Hangouts SMS enabled and different priorities.
- No priority set → BroadcastReceiver doesn't receive
SMS_RECEIVED
intent - Priority 500 → BroadcastReceiver does receive
SMS_RECEIVED
intent - Priority 9991 → BroadcastReceiver does receive
SMS_RECEIVED
intent
So it seems that you need to have a minimum value for priority in order to get the intent with Hangouts SMS enabled. I didn't bother to bisect the lowest possible value. ;) I am going with 999 as I don't see any reason to get lower because my app does just some quick checks on the received sms and does not further process it. But it should really make a difference, as the broadcast is non-abortable.
1The maximum value
Sending SMS with Intent not working in Oreo 8.1 Google API emulator
finally i found the answer myself:
public void sendSms(String phone, String sms) {
if(null != phone) {
Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto: " + phone));
i.putExtra("sms_body", sms);
if (i.resolveActivity(getPackageManager()) != null) {
startActivity(i);
}else{
Toast.makeText(this, "SMS App not found", Toast.LENGTH_LONG).show();
}
}
}
Send SMS message using non default SMS app on Android 4.4
You can send SMS even if your app isn't the default SMS app.
However, you won't be able to use the SMS provider.
That's the whole point of the new version - to make it clear to the user which app is allowed to use special SMS operations and have only one default app for this.
look at this text (taken from android developer blog) :
In consideration of some apps that do not want to behave as the
default SMS app but still want to send messages, any app that has the
SEND_SMS permission is still able to send SMS messages using
SmsManager. If and only if an app is not selected as the default SMS
app on Android 4.4, the system automatically writes the sent SMS
messages to the SMS Provider (the default SMS app is always
responsible for writing its sent messages to the SMS Provider).
so, in short, if all you need is to send and receive SMS, you can still do it.
however, some operations require that you will have your app as the default SMS app.
Related Topics
How to Test If a Fragment View Is Visible to the User
How to Change Separator Color in Navigationview
How to Get References for All Currently Active Fragments in an Activity
How to Send Notification to Specific Users with Fcm
App Completely Restarting When Launched by Icon Press in Launcher
Android Overlay to Grab All Touch, and Pass Them On
How to Implement Image Gallery in Gridview in Android
Mpandroid Chart Not Displaying Any Labels for Xaxis , What Is Missing
How to Remove the Top and Bottom Space on Textview of Android
Android Spannablestring Set Background Behind Part of Text
Android:How to Update the Selector(Statelistdrawable) Programmatically
How to Tell Which App Was Selected by Intent.Createchooser
Need to Disable Expand on Collapsingtoolbarlayout for Certain Fragments
Speed Control of Mediaplayer in Android