How to check if my application is the default launcher [duplicate]

[亡魂溺海] 提交于 2019-12-17 15:44:35

问题


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

回答1:


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



回答2:


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



回答3:


Kotlin version:

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


来源:https://stackoverflow.com/questions/8299427/how-to-check-if-my-application-is-the-default-launcher

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