如何以编程方式设置默认应用启动器?

por*_*der 39 android launcher android-intent

我正在创建一个可以通过谷歌下载的启动器(自助服务终端)应用程序.首次安装此应用程序时,用户可以选择哪个启动器(我的或库存)将是默认设置.如果用户没有将我的应用程序设置为默认启动器,我试图手动启动它.当对话框出现时,我希望用户强制选择ALWAYS而不是JUST ONCE,否则对话框将继续定期显示友好消息.这是我到目前为止所尝试的.

我创建了一个方法来检查我的应用程序是否是默认的

/**
 * method checks to see if app is currently set as default launcher
 * @return boolean true means currently set as default, otherwise false
 */ 
private boolean isMyAppLauncherDefault() {
    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();

    packageManager.getPreferredActivities(filters, activities, null);

    for (ComponentName activity : activities) {
        if (myPackageName.equals(activity.getPackageName())) {
            return true;
        }
    }
    return false;
}   
Run Code Online (Sandbox Code Playgroud)

然后我尝试启动选择器

/**
 * method starts an intent that will bring up a prompt for the user
 * to select their default launcher. It comes up each time it is
 * detected that our app is not the default launcher
 */
private void launchAppChooser() {
    Log.d(TAG, "launchAppChooser()");
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)

当我这样做时,我没有在我的应用程序和股票启动器之间做出选择.我尝试使用startActivity(Intent.createChooser(intent, "Please set launcher settings to ALWAYS"));,我在我的应用程序和股票启动器之间做出选择,但是,我没有得到选项总是或只是一次.

我可以为此创建一个自定义对话框,而不是启动选择器,但我需要知道如何以编程方式设置默认的应用程序启动器.提前致谢!

sas*_*oar 65

实际上这可以通过一些解决方法实现:

创建一个空的Activity充当已调用的启动器FakeLauncherActivity.将其作为禁用组件添加到清单中:

<activity
    android:name="com.path.to.your.FakeLauncherActivity"
    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)

检查您所需的启动器活动是否为默认启动器活动(isMyAppLauncherDefault()来自您的问题).

如果没有,请让用户选择首选的启动器活动,如下所示:

public static void resetPreferredLauncherAndOpenChooser(Context context) {
    PackageManager packageManager = context.getPackageManager();
    ComponentName componentName = new ComponentName(context, com.path.to.your.FakeLauncherActivity.class);
    packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

    Intent selector = new Intent(Intent.ACTION_MAIN);
    selector.addCategory(Intent.CATEGORY_HOME);
    selector.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(selector);

    packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);
}
Run Code Online (Sandbox Code Playgroud)

此方法暂时启用FakeLauncherActivity,这会导致可用启动器活动集的更改,从而迫使Android忘记其默认启动器.你会看到......

521-735/system_process I/PackageManager? Result set changed, dropping preferred activity for Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 } type null
Run Code Online (Sandbox Code Playgroud)

...在你的日志中.

然后,该方法只需打开一个启动器意图,您可以在其中查看所有已安装的启动器和"始终"/"仅一次"按钮.最后,该方法FakeLauncherActivity再次禁用,以便它不会显示在列表中.

您可以根据需要随时重复该操作,只有在您将所需的启动器活动设置为默认值时才让用户继续操作.

  • 我发现我的答案只是这个的副本:http://stackoverflow.com/a/13239706/1140682 ...所以请继续并提出请求.当我在这里回答时,我不知道我的解决方案在哪里,但现在我又找到了源码. (4认同)
  • 哇!毕竟这一次都是可行的答案!谢谢你先生! (2认同)
  • 我正在开发启动器应用程序,但在保留默认启动器作为我的启动器应用程序方面面临挑战,即我将启动器设置为默认启动器,重新启动设备后,设备再次要求我设置默认启动器。请指导我,以便我可以在我的项目中解决这个问题。提前致谢。 (2认同)

Zoh*_*har 8

问题中的isMyAppLauncherDefault()函数由于某种原因并不总是有效.此代码可能更适合确定HOME屏幕的默认包.

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
String currentHomePackage = resolveInfo.activityInfo.packageName;
Run Code Online (Sandbox Code Playgroud)


Com*_*are 7

当对话框出现时,我希望用户强制选择ALWAYS而不是JUST ONCE

这是不可能的,除了可能在root设备上,除非在Android中存在一些安全漏洞.

当我这样做时,我没有在我的应用程序和股票启动器之间做出选择

正确.如果已经选择了默认值,则只会启动默认值.

我尝试使用startActivity(Intent.createChooser(意图,"请将启动器设置设置为ALWAYS")); 我得到了我的应用程序和股票启动器之间的选择,但是,我没有得到选项总是或只是一次.

正确.createChooser()强制选择,但不允许设置默认值.

  • @portfoliobuilder:那么有时我会弄清楚他们是如何做到这一点的,然后向Google报告安全漏洞并尝试修复漏洞. (2认同)

归档时间:

查看次数:

43066 次

最近记录:

6 年,1 月 前