简体   繁体   English

当我的应用不是默认启动器应用时,调用“选择启动器”对话框

[英]Call “select launcher” dialog when my app isn't the default launcher app

I'm doing an app that is the default launcher of my device, after a lot of searches I found that isn't possible define programmatically my app as the default launcher. 我正在做一个应用程序,它是设备的默认启动器,经过大量搜索后,我发现不可能以编程方式将我的应用程序定义为默认启动器。 So I decided to do the following, I will enable to an admin user set this app as the default and this admin is going to be the person who will install the app and configure it at first time, before giving the device to another user. 因此,我决定执行以下操作,我将允许管理员用户将此应用程序设置为默认应用程序,并且在将设备交给其他用户之前,该管理员将是首次安装该应用程序并对其进行配置的人。 The only way to get out of app is pressing the back button, when user press it a dialog will appear asking for a password, if password typed is the right one the app will be removed as the default launcher 退出应用程序的唯一方法是按“后退”按钮,当用户按下该按钮时,将出现一个对话框,要求输入密码,如果键入的密码正确,则该应用程序将作为默认启动器被删除。

I used this line code to do this: 我使用以下代码执行此操作:

getPackageManager().clearPackagePreferredActivities(getPackageName());

This works fine and I can back to home screen of device normally, what i want is that, when an user got out of app and back to it, i want to verify if the app is the default launcher, and if it is not, show the "select launcher" dialog for choose it as default. 这可以正常工作,我可以正常返回设备的主屏幕,我想要的是,当用户退出应用程序并返回到该屏幕时,我想确认该应用程序是否为默认启动器,如果不是,显示“选择启动器”对话框以将其选择为默认设置。

I've tried all this options: 我已经尝试了所有这些选项:

Android home-screen/launcher chooser does not show 'use by default for this action' option Android主屏幕/启动器选择器不显示“默认情况下对此操作使用”选项

android: choose default launcher programatically android:以编程方式选择默认启动器

How to set default app launcher programmatically? 如何以编程方式设置默认应用启动器?

But any of this worked, it can't recognize that my app isn't more the default launcher. 但这一切都起作用,它无法识别我的应用不是默认的启动器。 But when i press the home button the dialog to select launcher appears. 但是,当我按下主页按钮时,将出现选择启动器的对话框。 I want when start my app again this dialog appear, without i press the home button. 我想在再次启动我的应用程序时出现此对话框,而无需按主页按钮。

this is the method i'm using to find if is my default launcher: 这是我用来查找是否是默认启动器的方法:

private boolean isMyLauncherDefault() {
    final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
    filter.addCategory(Intent.CATEGORY_HOME);

    List<IntentFilter> filters = new ArrayList<>();
    filters.add(filter);

    final String myPackageName = getPackageName();
    List<ComponentName> activities = new ArrayList<>();
    PackageManager packageManager = getPackageManager();

    // You can use name of your package here as third argument
    packageManager.getPreferredActivities(filters, activities, null);


    if(activities.size() == 0) //no default
        return true;

    for (ComponentName activity : activities) {
        if (myPackageName.equals(activity.getPackageName())) {
            return true;
        }
    }
    return false;
}

When i look at activities array on debug my app is still there, but how i said, android know that my app isn't more the default. 当我在调试时查看活动数组时,我的应用程序仍然存在,但是我怎么说,android知道我的应用程序不是默认的。 i need this method recognize this too. 我也需要这种方法认识到这一点。

And the following method suppose to call the dialog again: 并且以下方法假设再次调用该对话框:

public static void resetPreferredLauncherAndOpenChooser(Context context) {
    PackageManager packageManager = context.getPackageManager();
    packageManager.clearPackagePreferredActivities(context.getPackageName());
    ComponentName componentName = new ComponentName(context, VideoActivity.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);
}

All this methods came from previous links. 所有这些方法都来自以前的链接。

I will be very grateful if someone can help me. 如果有人可以帮助我,我将不胜感激。

PS: I'm using the sdk target version 21 PS:我使用的是sdk目标版本21

I Found the solution. 我找到了解决方案。 I used this answer: https://stackoverflow.com/a/7824190/2382100 我用这个答案: https : //stackoverflow.com/a/7824190/2382100

And my code look like this: 我的代码如下所示:

ComponentName componentName = new ComponentName(this, VideoActivity.class);
getPackageManager().setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED, PackageManager.DONT_KILL_APP);

This will disable my app , but if i understand well , it still going to be the default launcher but disabled, and it will back to home screen, after back to home screen, if user open it again, then the code will do this: 这将禁用我的应用程序,但如果我理解得很好,它仍将是默认启动器但已禁用,它将返回主屏幕,回到主屏幕后,如果用户再次打开它,则代码将执行此操作:

Log.d(TAG, "Recreating launcher");
PackageManager packageManager = context.getPackageManager();
packageManager.clearPackagePreferredActivities(context.getPackageName());
ComponentName componentName = new ComponentName(context,VideoActivity.class);
packageManager.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);

Works very well, glad of Palani's answer. 效果很好,很高兴Palani的回答。

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

相关问题 为 Android 7 中未出现的应用选择默认启动器 - Select default launcher for an app not appear in Android 7 默认启动器应用程序-如何检测其他应用程序是否始终在启动器中 - Default Launcher App - How to detect if another app is 'always' the launcher 启动器活动的权限。 应用未安装错误 - Permission on launcher activity. App isn't installed error 我的启动器应用程序没有自动更新 - No automatic update of my launcher app 应用程式未在启动器中显示 - App doesn't show in launcher 如何在 Flutter 中显示默认锁定屏幕而不是我的启动器应用程序? - How to show default lock screen instead of my launcher app in Flutter? 如何在 Android Studio 中打开 AOSP 默认启动器应用 Launcher3? - How to open AOSP default launcher app Launcher3 in Android Studio? 如何在按下主页时将我的应用设置为默认启动器,作为一项可选功能 - how to make my app as default launcher when press home, as an OPTIONAL feature 当应用程序在后台时,如何使用 firebase 通知更改默认启动器活动 - How to change default launcher activity with firebase notification when app in background 启动器应用选择器对话框未显示 - Launcher App chooser dialog is not being displayed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM