如何检查我的应用程序是否是默认启动器

时间:2011-11-28 17:01:47

标签: android launcher homescreen

我正在开发一个基本上是主屏幕的buissness-application,它应该被用作默认主屏幕(作为“kiosk”应用程序)。

有没有办法检查我的启动器是否是默认的启动器? 谢谢!

聚苯乙烯。 类似的例子,但用于检查GPS设置

LocationManager alm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
    Stuffs&Actions;
}

4 个答案:

答案 0 :(得分:25)

您可以从PackageManager获取首选活动列表。使用getPreferredActivities()方法。

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

答案 1 :(得分:5)

在这里找到我的答案:

Which launcher is running?

它告诉我我的活动是否是默认启动器..

答案 2 :(得分:0)

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 :(得分:0)

Kotlin版本:

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