how could I get default sms app's package name?

匿名 (未验证) 提交于 2019-12-03 08:57:35

问题:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!