清除并设置默认的主应用程序

r2D*_*Inc 29 java android launcher

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);
   }
Run Code Online (Sandbox Code Playgroud)

这里Intent i

Intent home = new Intent("android.intent.action.MAIN");
        home.addCategory("android.intent.category.HOME");
Run Code Online (Sandbox Code Playgroud)

然后我调用系统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);
   }
Run Code Online (Sandbox Code Playgroud)

选择器出现并正常运行,但它实际上并未设置或清除任何值.在调试时,似乎我错过了一些额外的东西?当我调用该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
Run Code Online (Sandbox Code Playgroud)

当我使用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
Run Code Online (Sandbox Code Playgroud)
  1. 我怎样才能进入那里并查看与该捆绑包一起发送的内容?
  2. 我怎样才能清除首选应用?不要告诉我你不能,我已经看到了足够的答案.诺瓦做到了,并且完全符合我想要的方式.

r2D*_*Inc 54

执行此操作的代码实际上只是一个非常聪明的工作.

当一个组件用

        <category android:name="android.intent.category.HOME" />
Run Code Online (Sandbox Code Playgroud)

启用,通常从安装新的家庭应用程序,默认的家庭应用程序被清除.

通过使用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>     
Run Code Online (Sandbox Code Playgroud)

如果要设置新的默认值,请启用此组件,然后调用home intent,然后再次禁用虚假的home组件.

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);
   }
Run Code Online (Sandbox Code Playgroud)

最终结果是系统认为安装了新的家庭应用程序,因此默认设置被清除,允许您设置没有特殊权限的用户.

感谢TeslaCoil和NovaLauncher的Kevin提供有关如何完成此操作的信息!

  • 这似乎不适用于4.4.2当另一个默认启动器已经设置时(任何人都可以确认?).启用虚假活动似乎没有区别,设备显示没有对话框,当它到达`c.startActivity(selector);`时,它返回到原始默认应用程序. (3认同)

Bru*_*uce 5

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);
Run Code Online (Sandbox Code Playgroud)


Chr*_*use 5

我在Android 4.1.2上将以下代码与工业平板电脑上的平台签名的信息亭模式应用程序配合使用。它使用不推荐使用PackageManager.addPreferredActivity(),但是优点是它可以在无需用户交互的情况下工作。在使用“ always”选项选择了标准的Android启动器后,它甚至可以工作。

// 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; }
Run Code Online (Sandbox Code Playgroud)