如何检查我的应用程序是否为默认启动程序
问题描述:
我正在开发基本上是主屏幕的应用程序,并且应该将其作为默认主屏幕(作为“自助服务终端”应用程序)使用。如何检查我的应用程序是否为默认启动程序
有什么办法检查我的启动器是否是默认的启动器? 谢谢!
Ps。 类似的例子,但对于检查GPS的设置
LocationManager alm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
Stuffs&Actions;
}
答
你可以从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;
}
答
+0
它只是告诉你,如果一个启动运行。如果Google Now Launcher和NOVA Launcher都在运行,则两者都将位于返回的列表中。 –
答
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;
}
答
科特林版本:
val Context.isMyLauncherDefault: Boolean
get() = ArrayList<ComponentName>().apply {
packageManager.getPreferredActivities(
arrayListOf(IntentFilter(ACTION_MAIN).apply { addCategory(CATEGORY_HOME) }),
this,
packageName
)
}.isNotEmpty()
工作正常。我使用包名作为第三个参数,然后检查'activities'列表的长度。如果它是0,则表示不启动。 –
在这种情况下,“活动”会在这个查询中填充多个项目吗? –
[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)表明第一个参数应该是一个由该方法填充的空列表。当你给出一个已经在你的例子中填充的列表时,究竟是什么行为? – achoo5000