繁体   English   中英

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

[英]How to set default app launcher programmatically?

我正在创建一个可通过谷歌下载的启动器(信息亭)应用程序。 首次安装此应用程序时,用户可以选择默认启动器(我的或股票)。 如果用户没有将我的应用程序设置为默认启动器,我将尝试手动启动它。 我希望用户在该对话框出现时被迫选择始终而不是只选择一次,否则对话框将继续定期出现并显示友好消息。 这是我迄今为止所尝试的。

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

/**
 * 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;
}   

然后我尝试启动选择器

/**
 * 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);
}

当我这样做时,我没有收到我的应用程序和股票启动器之间的选择。 我尝试使用startActivity(Intent.createChooser(intent, "Please set launcher settings to ALWAYS")); 我可以在我的应用程序和股票启动器之间进行选择,但是,我没有得到 ALWAYS 或 JUST ONCE 选项。

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

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

创建一个空的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>

检查所需的启动器活动是否为默认启动器活动(使用您问题中的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);
}

此方法暂时启用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

...在你的日志中。

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

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

问题中的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;

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

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

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

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

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

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

如果您正在实施 Google EMM 解决方案(专用设备):

https://developer.android.com/work/dpc/dedicated-devices/cookbook

// Create an intent filter to specify the Home category.
IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_HOME);
filter.addCategory(Intent.CATEGORY_DEFAULT);

// Set the activity as the preferred option for the device.
ComponentName activity = new ComponentName(context, KioskModeActivity.class);
DevicePolicyManager dpm =
    (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
dpm.addPersistentPreferredActivity(adminName, filter, activity);

Android Q (API 29) 有RoleManager 假设您的应用是启动器。

[AndroidManifest.xml]
<activity
    ... />

    <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>


private val startForResult = registerForActivityResult(
    ActivityResultContracts.StartActivityForResult()
) { activityResult ->
    if (activityResult.resultCode == Activity.RESULT_OK) {
        // Perhaps log the result here.
    }
}

private fun showLauncherSelection() {
    val roleManager = requireActivity().getSystemService(Context.ROLE_SERVICE)
            as RoleManager
    if (roleManager.isRoleAvailable(RoleManager.ROLE_HOME) &&
        !roleManager.isRoleHeld(RoleManager.ROLE_HOME)
    ) {
        val intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_HOME)
        startForResult.launch(intent)
    }
}

当您调用showLauncherSelection()时,您应该会看到一个类似于以下内容的对话框。

在此处输入图像描述

还有其他角色,例如ROLE_BROWSERROLE_DIALERROLE_SMS等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM