Nova如何管理这个?我真的试图做同样的事情:为用户提供一个按钮来清除并选择他们的新默认启动器。
我可以获取默认的应用名称并显示它:
private String getPrefered(Intent i) {
PackageManager pm = this.getActivity().getPackageManager();
final ResolveInfo mInfo = pm.resolveActivity(i, 0);
return (String) pm.getApplicationLabel(mInfo.activityInfo.applicationInfo);
}
其中Intent i
是
Intent home = new Intent("android.intent.action.MAIN");
home.addCategory("android.intent.category.HOME");
然后我调用系统ResolveActivity,
private void makePrefered() {
Intent selector = new Intent("android.intent.action.MAIN");
selector.addCategory("android.intent.category.HOME");
selector.setComponent(new ComponentName("android", "com.android.internal.app.ResolverActivity"));
startActivity(selector);
}
选择器出现并正常运行,但它实际上并未设置或清除任何值。在调试时,似乎我错过了一些额外的东西?当我调用makePrefered
方法时,我收到以下日志消息,
I/ActivityManager( 602): START {act=android.intent.action.MAIN cat=[android.intent.category.HOME] cmp=android/com.android.internal.app.ResolverActivity u=0} from pid 22641
当我使用Nova实现时,我看到了所有这些,
I/PackageManager( 602): Result set changed, dropping preferred activity for Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 (has extras) } type null
I/ActivityManager( 602): START {act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=android/com.android.internal.app.ResolverActivity (has extras) u=0} from pid 22905
I/ActivityManager( 602): START {act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10200000 cmp=com.mycolorscreen.canvas/.Launcher (has extras) u=0} from pid 22905
答案 0 :(得分:48)
执行此操作的代码实际上只是一个非常聪明的工作。
当组件带有
时 <category android:name="android.intent.category.HOME" />
启用,通常从安装新的家庭应用程序,默认的家庭应用程序被清除。
通过像这样使用home组件创建一个空活动来利用这一点。
<activity
android:name="com.t3hh4xx0r.haxlauncher.FakeHome"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
如果要设置新默认值,请启用此组件,然后调用home intent 然后再次禁用虚假主页组件。
public static void makePrefered(Context c) {
PackageManager p = c.getPackageManager();
ComponentName cN = new ComponentName(c, FakeHome.class);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Intent selector = new Intent(Intent.ACTION_MAIN);
selector.addCategory(Intent.CATEGORY_HOME);
c.startActivity(selector);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
最终结果是系统认为安装了新的家庭应用程序,因此默认设置被清除,允许您设置没有特殊权限的用户。
感谢TeslaCoil和NovaLauncher的Kevin提供有关如何完成这项工作的信息!
答案 1 :(得分:4)
我在Android 4.1.2上使用以下代码,在工业平板电脑上使用平台签名的kiosk-mode应用程序。它使用已弃用的// Requires permission SET_PREFERRED_APPLICATIONS.
public static boolean setPreferredHomeActivity (Context context, String packageName, String className) {
ComponentName oldPreferredActivity = getPreferredHomeActivity(context);
if (oldPreferredActivity != null && packageName.equals(oldPreferredActivity.getPackageName()) && className.equals(oldPreferredActivity.getClassName())) {
return false; }
if (oldPreferredActivity != null) {
context.getPackageManager().clearPackagePreferredActivities(oldPreferredActivity.getPackageName()); }
IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_HOME);
filter.addCategory(Intent.CATEGORY_DEFAULT);
ComponentName[] currentHomeActivities = getActivitiesListByActionAndCategory(context, Intent.ACTION_MAIN, Intent.CATEGORY_HOME);
ComponentName newPreferredActivity = new ComponentName(packageName, className);
context.getPackageManager().addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY, currentHomeActivities, newPreferredActivity);
return true; }
private static ComponentName getPreferredHomeActivity (Context context) {
ArrayList<IntentFilter> filters = new ArrayList<>();
List<ComponentName> componentNames = new ArrayList<>();
context.getPackageManager().getPreferredActivities(filters, componentNames, null);
for (int i = 0; i < filters.size(); i++) {
IntentFilter filter = filters.get(i);
if (filter.hasAction(Intent.ACTION_MAIN) && filter.hasCategory(Intent.CATEGORY_HOME)) {
return componentNames.get(i); }}
return null; }
private static ComponentName[] getActivitiesListByActionAndCategory (Context context, String action, String category) {
Intent queryIntent = new Intent(action);
queryIntent.addCategory(category);
List<ResolveInfo> resInfos = context.getPackageManager().queryIntentActivities(queryIntent, PackageManager.MATCH_DEFAULT_ONLY);
ComponentName[] componentNames = new ComponentName[resInfos.size()];
for (int i = 0; i < resInfos.size(); i++) {
ActivityInfo activityInfo = resInfos.get(i).activityInfo;
componentNames[i] = new ComponentName(activityInfo.packageName, activityInfo.name); }
return componentNames; }
,但其优点是无需用户交互即可运行。它甚至可以在标准的Android启动器选择&#34; always&#34;之后工作。选项。
{{1}}
答案 2 :(得分:3)
r2DoesInc的解决方案不适用于我的4.2.2测试设备。
我的解决方案:停用然后重新启用我的应用的HomeActivity,它不必创建FakeHome
PackageManager p = getPackageManager();
ComponentName cN = new ComponentName(this, HomeActivity.class);
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));
p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
答案 3 :(得分:1)
进一步采用@Bruce的答案(不使用假的家庭活动),可以使用PackageManager.setComponentEnabledSetting首先禁用组件,然后针对家庭意图使用resolveActivity(而不是使用startActivity),然后启用该组件,然后使用目的。
Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
PackageManager pm = getPackageManager();
ResolveInfo rInfo = pm.resolveActivity(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (!rInfo.activityInfo.packageName.equals(getPackageName())) { // your app is not the default HOME
ComponentName cn = <ComponentName object of your home activity>
pm.setComponentEnabledSetting(cn, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
pm.resolveActivity(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
pm.setComponentEnabledSetting(cn, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
startActivity(homeIntent);
}