21

I am developing a buissness-application that is essentially a Home-screen, and is supposed to be used as a Default Homescreen (being a "kiosk"-application).

Is there any way of checking if my Launcher is the default Launcher? Thanks!

Ps. Similar example, but for checking GPS-settings

LocationManager alm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
    Stuffs&Actions;
}
DagW
  • 955
  • 1
  • 15
  • 28

3 Answers3

30

You can get list of preferred activities from PackageManager. Use getPreferredActivities() method.

boolean isMyLauncherDefault() {
    final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
    filter.addCategory(Intent.CATEGORY_HOME);

    List<IntentFilter> filters = new ArrayList<IntentFilter>();
    filters.add(filter);

    final String myPackageName = getPackageName();
    List<ComponentName> activities = new ArrayList<ComponentName>();
    final PackageManager packageManager = (PackageManager) getPackageManager();

    // You can use name of your package here as third argument
    packageManager.getPreferredActivities(filters, activities, null);

    for (ComponentName activity : activities) {
        if (myPackageName.equals(activity.getPackageName())) {
            return true;
        }
    }
    return false;
}
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
  • Works fine. I used the package name as third parameter and then checked the length of the `activities` list. If it is 0, means not launcher. – Mister Smith Sep 25 '13 at 16:01
  • on which cases would "activities" be filled with more than one item in this query? – android developer Jan 21 '14 at 20:00
  • 2
    The documentation for [getPreferredActivities](http://developer.android.com/reference/android/content/pm/PackageManager.html#getPreferredActivities%28java.util.List%3Candroid.content.IntentFilter%3E,%20java.util.List%3Candroid.content.ComponentName%3E,%20java.lang.String%29) suggests that the first argument should be an empty list that is populated by the method. What exactly is the behavior when you are giving a list that is already populated as in your example? – achoo5000 Feb 01 '14 at 20:50
  • 5
    I checked the android [source code](https://github.com/android/platform_frameworks_base/blob/master/services/java/com/android/server/pm/PackageManagerService.java#L10179). There is no effect from adding a filter as you do in your example. I believe a better way to check for a default activity is to use [resolveActivity](http://developer.android.com/reference/android/content/pm/PackageManager.html#resolveActivity%28android.content.Intent,%20int%29) as in [this answer](http://stackoverflow.com/a/6832568/697611) to a similar question. – achoo5000 Feb 01 '14 at 21:34
  • How would you find all of the preferred activities for all of the intents that have them? You would need to go over all of the apps, prepare a filter for each intent on the app's manifest, and then use this method? – android developer Aug 26 '14 at 23:22
  • It works, you could test it simple with whis code: isMyLauncherDefault(); Toast.makeText(getApplicationContext(),"Value:" + isMyLauncherDefault(), Toast.LENGTH_SHORT).show(); – Pavel Kostal Sep 19 '19 at 13:29
3
boolean isHomeApp() {
    final Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    final ResolveInfo res = getPackageManager().resolveActivity(intent, 0);
    if (res.activityInfo != null && getPackageName()
            .equals(res.activityInfo.packageName)) {
        return true;
    }
    return false;
}
Oded Breiner
  • 28,523
  • 10
  • 105
  • 71
1

Kotlin version:

val Context.isMyLauncherDefault: Boolean
  get() = ArrayList<ComponentName>().apply {
    packageManager.getPreferredActivities(
      arrayListOf(IntentFilter(ACTION_MAIN).apply { addCategory(CATEGORY_HOME) }),
      this,
      packageName
    )
  }.isNotEmpty()