我想获得Android L中的所有默认应用程序。我使用了以下代码,但它们给了我一个错误的解决方案。让我们先看看我的代码
private void getMyAppLauncherDefault() {
final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_HOME);
List<IntentFilter> filters = new ArrayList<IntentFilter>();
filters.add(filter);
List<ComponentName> activities = new ArrayList<ComponentName>();
final PackageManager packageManager = (PackageManager) getPackageManager();
packageManager.getPreferredActivities(filters, activities, null);
for (ComponentName activity : activities) {
Log.d(TAG,"======packet default:==="+activity.getPackageName());
}
}
这是日志。该日志显示com.google.android.googlequicksearchbox
和com.vlingo.midas
之间的错误结果。它们都是语音应用,但我将com.google.android.googlequicksearchbox
设置为默认值。我不知道为什么日志会显示com.vlingo.midas
。我该如何解决?感谢
16:02:44.817 /com.exam D/Sample: ======packet default:===com.sec.android.gallery3d
16:02:44.827 /com.exam D/Sample: ======packet default:===com.android.mms
16:02:44.827 /com.exam D/Sample: ======packet default:===com.android.mms
16:02:44.827 /com.exam D/Sample: ======packet default:===com.vlingo.midas
16:02:44.827 /com.exam D/Sample: ======packet default:===com.sec.android.app.sbrowser
16:02:44.827 /com.exam D/Sample: ======packet default:===com.sec.android.gallery3d
16:02:44.827 /com.exam D/Sample: ======packet default:===com.android.mms
16:02:44.827 /com.exam D/Sample: ======packet default:===com.sec.android.app.launcher
16:02:44.827 /com.exam D/Sample: ======packet default:===com.sec.android.app.sbrowser
16:02:44.827 /com.exam D/Sample: ======packet default:===com.android.mms
16:02:44.827 /com.exam D/Sample: ======packet default:===com.google.android.googlequicksearchbox
16:02:44.827 /com.exam D/Sample: ======packet default:===com.sec.android.app.sbrowser
16:02:44.827 /com.exam D/Sample: ======packet default:===com.sec.android.gallery3d
16:02:44.827 /com.exam D/Sample: ======packet default:===com.android.mms
16:02:44.827 /com.exam D/Sample: ======packet default:===com.google.android.apps.plus
答案 0 :(得分:2)
检查您的应用是否设置为&#34;默认&#34;那么请试试这段代码:
public static boolean isMyAppDefault(Context context) {
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 = context.getPackageName();
List<ComponentName> activities = new ArrayList<ComponentName>();
final PackageManager packageManager = (PackageManager) context.getPackageManager();
packageManager.getPreferredActivities(filters, activities, null);
for (ComponentName activity : activities) {
if (myPackageName.equals(activity.getPackageName())) {
return true;
}
}
return false;
}
答案 1 :(得分:2)
您在上面添加的代码是完全正确的。它确实表现了它的意图。
现在您已将com.google.android.googlequicksearchbox
语音应用设置为默认值,这就是它在日志中显示的原因。
虽然显示com.vlingo.midas
可能是因为它被设置为其他类别而非语音的默认值。
答案 2 :(得分:-1)
我正在使用此方法检查默认启动器。
public void foo(){
String defaultHomeApp = getDefaultAppByCategory(context, Intent.CATEGORY_HOME);
...
}
private String getDefaultAppByCategory(Context context, String category) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(category);
ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
return resolveInfo.activityInfo.packageName;
}
您可以使用Intent
来获取用户默认使用的应用程序。
String packageApp= context.getPackageName();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(<ADD_HERE_THE_CATEGORY>);
ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
resolveInfo
变量将具有该类别的默认软件包。