Search code examples
androidandroid-intentandroid-activityhomescreen

How to get default sms,gallery,call,contacts and browser app and package name in android?


I am creating an home replacement app for android(Launcher) and I want to place the sms , call, contacts ,gallery and browser apps in the home screen. How can I know the package name for them.

If the user is using a custom contact app as the default one , I need to get the package name of that one and not the android contact app.

How can I achieve this?Thanks.


Solution

  • You can use this code to get Intent to those thing. The problomatic one is the SMS one.

    For sms:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(ctx);
                    Intent lunchIntent;
                    if (defaultSmsPackageName != null) {
                        launchIntent = pm.getLaunchIntentForPackage(defaultSmsPackageName);
                    } else {
                        String SMS_MIME_TYPE = "vnd.android-dir/mms-sms";
                        launchIntent = new Intent(Intent.ACTION_MAIN);
                        launchIntent.setType(SMS_MIME_TYPE);
                    }
                } else {
                    String SMS_MIME_TYPE = "vnd.android-dir/mms-sms";
                    launchIntent = new Intent(Intent.ACTION_MAIN);
                    launchIntent.setType(SMS_MIME_TYPE);
                }
    

    For call:

         Intent intent = new Intent(Intent.ACTION_DIAL);
    

    For browser:

    Intent intent;
    Intent queryIntent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse("http://www.google.com"));
                    ActivityInfo af = queryIntent.resolveActivityInfo(pm, 0);
                    intent = new Intent(Intent.ACTION_MAIN);
                    intent.setClassName(af.packageName, af.name);
    

    For photos:

     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://media/internal/images/media"));
                    try {
                        context.startActivity(intent);
                    } catch (ActivityNotFoundException eee){
                        try {
                            intent = new Intent(Intent.ACTION_VIEW);
                            intent.setType(android.provider.MediaStore.Images.Media.CONTENT_TYPE);
                        } catch (Exception err){
                            Toast.makeText(context, "This app not supported in your device", Toast.LENGTH_LONG).show();
                        }
                    }