我怎么能得到默认短信应用程序的包名?

the*_*r0w 4 sms android

由于默认短信应用程序在4.4中添加,我无法打开默认的短信应用程序,如下所示:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

那我怎么能得到默认短信应用程序的包名,所以我可以直接从我的应用程序打开它?

For*_*rce 10

接受的答案对我不起作用(而且似乎相当不可靠).

获取包名称的更好方法是

Telephony.Sms.getDefaultSmsPackage(context);
Run Code Online (Sandbox Code Playgroud)

这需要API 19+


the*_*r0w 6

// 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);
}
Run Code Online (Sandbox Code Playgroud)