可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
As default sms app is added in 4.4, I cannot open default sms app like this:
Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setType("vnd.android-dir/mms-sms"); startActivity(intent);
So how could I get default sms app's package name, so I can open it from my app directly?
回答1:
//http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/android/provider/Settings.java#Settings.Secure.0SMS_DEFAULT_APPLICATION
public static final String SMS_DEFAULT_APPLICATION = "sms_default_application";
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/com/android/internal/telephony/SmsApplication.java#267
if(Utils.hasKikKat()) { String defaultApplication = Settings.Secure.getString(getContentResolver(), SMS_DEFAULT_APPLICATION); PackageManager pm = context.getPackageManager(); Intent intent = pm.getLaunchIntentForPackage(defaultApplication ); if (intent != null) { context.startActivity(intent); } } else { Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setType("vnd.android-dir/mms-sms"); startActivity(intent); }
回答2:
The accepted answer did not work for me (and seems rather unreliable).
A better way to get the package name is
Telephony.Sms.getDefaultSmsPackage(context);
This requires API 19+
回答3:
Here's how you do it:
@Nullable public static String getDefaultSmsAppPackageName(@NonNull Context context) { if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) return Telephony.Sms.getDefaultSmsPackage(context); 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; } }