android如何获取默认的桌面程序
【方法1】
http://stackoverflow.com/questions/12594192/remove-activity-as-default-launcher/12594332#12594332
桌面应用的启动在INTENT中需要包含ACTION_MAIN 和CATEGORY_HOME.
通过PackageManager的resolveActivity方法来获取一个 ResolveInfo 对象来得知哪个是默认启动的Activity
private void getDefaultHome() {final Intent intent = new Intent(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_HOME);final ResolveInfo res = getPackageManager().resolveActivity(intent, 0);if (res.activityInfo == null) {Log.d(TAG, "resolveActivity--->activityInfo null");// should not happen. A home is always installed, isn't it?} else if (res.activityInfo.packageName.equals("android")) {// No default selectedLog.d(TAG, "resolveActivity--->无默认设置");} else {// res.activityInfo.packageName and res.activityInfo.name gives// you the default appLog.d(TAG, "默认桌面为:" + res.activityInfo.packageName + "."+ res.activityInfo.name);}}
【方法2】
http://stackoverflow.com/questions/8299427/how-to-check-if-my-application-is-the-default-launcher/8361115#8361115
利用PackageManager里的getPreferredActivities()方法
boolean isMyLauncherDefault() {final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);filter.addCategory(Intent.CATEGORY_HOME);Listfilters = new ArrayList ();filters.add(filter);final String myPackageName = getPackageName();List activities = new ArrayList ();final PackageManager packageManager = (PackageManager) getPackageManager();// You can use name of your package here as third argumentpackageManager.getPreferredActivities(filters, activities, null);for (ComponentName activity : activities) {if (myPackageName.equals(activity.getPackageName())) {return true;}}return false; }
转载于:https://www.cnblogs.com/lqstayreal/p/3192712.html
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
