How can I get the default sms app package name?

By default sms app added in 4.4, I can't 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 can I get the sms default app package name so I can open it directly from my app?

+3


source to share


3 answers


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

      

+6


source


The accepted answer didn't work for me (and seems pretty unreliable).

Best way to get package name



Telephony.Sms.getDefaultSmsPackage(context);

      

This requires API 19+

+4


source


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

      

+1


source







All Articles