【问题标题】:How to programmatically enable auto start and floating window permissions如何以编程方式启用自动启动和浮动窗口权限
【发布时间】:2018-07-25 19:24:22
【问题描述】:
  1. 如何以编程方式启用自动启动权限?
  2. 如何找到需要做自动开机码的手机?
  3. 如何查看自动启动权限是启用还是禁用?

我只能找到关于具有canDrawOverlay() 权限的显示弹出权限

如果设备未启用,我想启用自动启动


我已经为小米找到了解决方案,荣誉与让步。

if(Build.BRAND.equalsIgnoreCase("xiaomi") ){

                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
                startActivity(intent);


            }else if(Build.BRAND.equalsIgnoreCase("Letv")){

                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.letv.android.letvsafe", "com.letv.android.letvsafe.AutobootManageActivity"));
                startActivity(intent);

            }
            else if(Build.BRAND.equalsIgnoreCase("Honor")){

                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.huawei.systemmanager", "com.huawei.systemmanager.optimize.process.ProtectActivity"));
                startActivity(intent);

            }

【问题讨论】:

  • 可能这是不可能的,但必须有意图将用户发送到适当的页面,用户可以在华为或小米设备中手动打开它。
  • 我知道,但是对于小米、华为、乐视,我们可以借助这些包名来实现这一点
  • oppo 设备呢?有OPPO设备的解决方案吗??
  • @Shaifali Rajput 试试这个可能对你有帮助 我没有测试过这个检查并通知Intent intent = new Intent(); intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity" )); startActivity(intent);
  • @Sagar2869767 谢谢我找到了解决方案,我也在这里发布了stackoverflow.com/questions/41804070/…

标签: android android-permissions autostart


【解决方案1】:

https://github.com/XomaDev/MIUI-autostart

嘿,试试这个库:D

Autostart autostart = new Autostart(applicationContext);

if (autostart.getAutoStartState() == State.ENABLED) {
    // autostart is enabled for sure
}

【讨论】:

  • 小心尝试使用反射访问方法,因为它可能会导致运行时错误(以防 Google Android 删除 API)
  • 该类只存在于MI设备,MIUI 12不存在,所以应该没有问题。
  • 已经更新了帖子,现在已经足够稳定,以后不会出问题了。
【解决方案2】:

请检查以下解决方案,为OPPOVIVO 设备启用floating windowautostart permission

无法确定自动启动选项是否启用。您可以在Security permissions => Autostart => Enable Autostart下手动查看。

在 Oppo 设备上:

  private void initOPPO() {
    try {

        Intent i = new Intent(Intent.ACTION_MAIN);
        i.setComponent(new ComponentName("com.oppo.safe", "com.oppo.safe.permission.floatwindow.FloatWindowListActivity"));
        startActivity(i);
    } catch (Exception e) {
        e.printStackTrace();
        try {

            Intent intent = new Intent("action.coloros.safecenter.FloatWindowListActivity");
            intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.floatwindow.FloatWindowListActivity"));
            startActivity(intent);
        } catch (Exception ee) {

            ee.printStackTrace();
            try{

                Intent i = new Intent("com.coloros.safecenter");
                i.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.sysfloatwindow.FloatWindowListActivity"));
                startActivity(i);
            }catch (Exception e1){

                e1.printStackTrace();
            }
        }

    }
}

VIVO 的自动启动权限

 private static void autoLaunchVivo(Context context) {
    try {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.iqoo.secure",
                "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity"));
        context.startActivity(intent);
    } catch (Exception e) {
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.vivo.permissionmanager",
                    "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
            context.startActivity(intent);
        } catch (Exception ex) {
            try {
                Intent intent = new Intent();
                intent.setClassName("com.iqoo.secure",
                        "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager");
                context.startActivity(intent);
            } catch (Exception exx) {
                ex.printStackTrace();
            }
        }
    }
}

OPPO 自动启动

 if (Build.MANUFACTURER.equalsIgnoreCase("oppo")) {
        try {
            Intent intent = new Intent();
            intent.setClassName("com.coloros.safecenter",
                    "com.coloros.safecenter.permission.startup.StartupAppListActivity");
            startActivity(intent);
        } catch (Exception e) {
            try {
                Intent intent = new Intent();
                intent.setClassName("com.oppo.safe",
                        "com.oppo.safe.permission.startup.StartupAppListActivity");
                startActivity(intent);

            } catch (Exception ex) {
                try {
                    Intent intent = new Intent();
                    intent.setClassName("com.coloros.safecenter",
                            "com.coloros.safecenter.startupapp.StartupAppListActivity");
                    startActivity(intent);
                } catch (Exception exx) {

                }
            }
        }
}

【讨论】:

  • 非常感谢。伟大的它也为我工作。但是如何在vivo设备中自动启动应用程序而不是弹出浮动窗口权限呢?我想在内部像 OPPO 一样做同样的事情,它想启用你上面提到的 OPPO。
  • 如何检查自动启动权限是否以编程方式启用或禁用?
  • 我在我的应用程序中做了同样的事情,但 VIVO 仍然有一些问题。它仍然会杀死我的后台服务
  • 在 oppo 中是个例外。该怎么办? @Sagar
  • @Makvin 不,你只能用你能做的所有代码来启动auto start activity
【解决方案3】:

正如其他人所说无法确定是否启用了自动启动选项,但我们可以使用意图将用户重定向到自动启动设置。然后由用户决定是否允许。

我们可以使用ACTION_APPLICATION_DETAILS_SETTINGSACTION_MANAGE_APPLICATIONS_SETTINGS 标志直接重定向到自动启动设置屏幕。

我已在 XiaomiOPPO 手机上对此进行了测试,我相信此代码也适用于 Vivo 等其他自定义 UI 设备。

点击弹出对话框时,请从应用设置屏幕中检查并启用自动启动选项。将下面的代码放在点击确定按钮上。

  try
    {
        //Open the specific App Info page:
        Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + context.getPackageName()));
        context.startActivity(intent);
    }
    catch ( ActivityNotFoundException e )
    {
        //Open the generic Apps page:
        Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
        context.startActivity(intent);
    }

【讨论】:

  • 这适用于 oppo realme c2、htc hope 和 samsung s5(因为 htc 和 samsung 中没有自动启动启用选项,但它仍会打开应用程序设置屏幕)...我希望它适用于其他自定义设备也是如此
  • 您是否在上述设备以外的设备上测试过它?
  • @syed_noorullah 我已经在股票 android (PIE)、Honor EMUI、oppo(color os)、Xiaomi (MIUI) 上对其进行了测试,我还发现华为和 Honor(两者都是同一家公司并且有相同的自定义操作系统)。上面的代码正在进入应用程序信息屏幕,但没有可用的自动运行选项。我想知道他们正在通过杀死除白名单应用程序之外的所有服务来优化手机的后台服务,因此很难在华为或荣誉设备中对事物进行分类,如果您发现了什么,请分享。
  • 在同一线程中查看 Adarsh binjola 的答案。我认为他为华为荣誉提供了自动启动活动:- private final String BRAND_HONOR = "honor"; private String PACKAGE_HONOR_MAIN = "com.huawei.systemmanager"; private String PACKAGE_HONOR_COMPONENT = "com.huawei.systemmanager.optimize.process.ProtectActivity";
【解决方案4】:

如果小米操作系统需要它,当您从 playstore 下载应用程序时,自动启动功能将自动启用,因为亚马逊、谷歌 IO 等应用程序也不允许自动启动,在这种情况下,您必须转到安全权限 ->自动启动->然后从那里启用自动启动。您不能通过代码使应用程序自动启动,您所能做的就是显示一个对话框以启用自动启动并将用户带到自动启动活动,但这不是一个好的选择,因为您无法检查是否启用自动启动。

这是小米在MIUI8中为了省电而做的。

参考

可以参考文章MIUI8

【讨论】:

  • 我们不能以编程方式进行啊?
  • 不,您只能使用您能做的所有代码来启动自动启动活动
【解决方案5】:

使用这个帮助类

 public class AutoStartHelper {

/***
 * Xiaomi
 */
private final String BRAND_XIAOMI = "xiaomi";
private String PACKAGE_XIAOMI_MAIN = "com.miui.securitycenter";
private String PACKAGE_XIAOMI_COMPONENT = "com.miui.permcenter.autostart.AutoStartManagementActivity";

/***
 * Letv
 */
private final String BRAND_LETV = "letv";
private String PACKAGE_LETV_MAIN = "com.letv.android.letvsafe";
private String PACKAGE_LETV_COMPONENT = "com.letv.android.letvsafe.AutobootManageActivity";

/***
 * ASUS ROG
 */
private final String BRAND_ASUS = "asus";
private String PACKAGE_ASUS_MAIN = "com.asus.mobilemanager";
private String PACKAGE_ASUS_COMPONENT = "com.asus.mobilemanager.powersaver.PowerSaverSettings";

/***
 * Honor
 */
private final String BRAND_HONOR = "honor";
private String PACKAGE_HONOR_MAIN = "com.huawei.systemmanager";
private String PACKAGE_HONOR_COMPONENT = "com.huawei.systemmanager.optimize.process.ProtectActivity";

/**
 * Oppo
 */
private final String BRAND_OPPO = "oppo";
private String PACKAGE_OPPO_MAIN = "com.coloros.safecenter";
private String PACKAGE_OPPO_FALLBACK = "com.oppo.safe";
private String PACKAGE_OPPO_COMPONENT = "com.coloros.safecenter.permission.startup.StartupAppListActivity";
private String PACKAGE_OPPO_COMPONENT_FALLBACK = "com.oppo.safe.permission.startup.StartupAppListActivity";
private String PACKAGE_OPPO_COMPONENT_FALLBACK_A = "com.coloros.safecenter.startupapp.StartupAppListActivity";

/**
 * Vivo
 */

private final String BRAND_VIVO = "vivo";
private String PACKAGE_VIVO_MAIN = "com.iqoo.secure";
private String PACKAGE_VIVO_FALLBACK = "com.vivo.perm;issionmanager";
private String PACKAGE_VIVO_COMPONENT = "com.iqoo.secure.ui.phoneoptimize.AddWhiteListActivity";
private String PACKAGE_VIVO_COMPONENT_FALLBACK = "com.vivo.permissionmanager.activity.BgStartUpManagerActivity";
private String PACKAGE_VIVO_COMPONENT_FALLBACK_A = "com.iqoo.secure.ui.phoneoptimize.BgStartUpManager";

/**
 * Nokia
 */

private final String BRAND_NOKIA = "nokia";
private String PACKAGE_NOKIA_MAIN = "com.evenwell.powersaving.g3";
private String PACKAGE_NOKIA_COMPONENT = "com.evenwell.powersaving.g3.exception.PowerSaverExceptionActivity";


private AutoStartHelper() {
}

public static AutoStartHelper getInstance() {
    return new AutoStartHelper();
}


public void getAutoStartPermission(Context context) {

    String build_info = Build.BRAND.toLowerCase();
    switch (build_info) {
        case BRAND_ASUS:
            autoStartAsus(context);
            break;
        case BRAND_XIAOMI:
            autoStartXiaomi(context);
            break;
        case BRAND_LETV:
            autoStartLetv(context);
            break;
        case BRAND_HONOR:
            autoStartHonor(context);
            break;
        case BRAND_OPPO:
            autoStartOppo(context);
            break;
        case BRAND_VIVO:
            autoStartVivo(context);
            break;
        case BRAND_NOKIA:
            autoStartNokia(context);
            break;

    }

}

private void autoStartAsus(final Context context) {
    if (isPackageExists(context, PACKAGE_ASUS_MAIN)) {

        showAlert(context, (dialog, which) -> {
            try {
            PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                startIntent(context, PACKAGE_ASUS_MAIN, PACKAGE_ASUS_COMPONENT);
            } catch (Exception e) {
                e.printStackTrace();
            }
            dialog.dismiss();
        });

    }


}

private void showAlert(Context context, DialogInterface.OnClickListener onClickListener) {

    new AlertDialog.Builder(context).setTitle("Allow AutoStart")
            .setMessage("Please enable auto start in settings.")
            .setPositiveButton("Allow", onClickListener).show().setCancelable(false);
}

private void autoStartXiaomi(final Context context) {
    if (isPackageExists(context, PACKAGE_XIAOMI_MAIN)) {
        showAlert(context, (dialog, which) -> {
            try {
                PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                startIntent(context, PACKAGE_XIAOMI_MAIN, PACKAGE_XIAOMI_COMPONENT);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });


    }
}

private void autoStartLetv(final Context context) {
    if (isPackageExists(context, PACKAGE_LETV_MAIN)) {
        showAlert(context, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                try {
                    PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                    startIntent(context, PACKAGE_LETV_MAIN, PACKAGE_LETV_COMPONENT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });


    }
}


private void autoStartHonor(final Context context) {
    if (isPackageExists(context, PACKAGE_HONOR_MAIN)) {
        showAlert(context, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                try {
                    PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                    startIntent(context, PACKAGE_HONOR_MAIN, PACKAGE_HONOR_COMPONENT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });


    }
}

private void autoStartOppo(final Context context) {
    if (isPackageExists(context, PACKAGE_OPPO_MAIN) || isPackageExists(context, PACKAGE_OPPO_FALLBACK)) {
        showAlert(context, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                try {
                    PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                    startIntent(context, PACKAGE_OPPO_MAIN, PACKAGE_OPPO_COMPONENT);
                } catch (Exception e) {
                    e.printStackTrace();
                    try {
                        PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                        startIntent(context, PACKAGE_OPPO_FALLBACK, PACKAGE_OPPO_COMPONENT_FALLBACK);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        try {
                            PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                            startIntent(context, PACKAGE_OPPO_MAIN, PACKAGE_OPPO_COMPONENT_FALLBACK_A);
                        } catch (Exception exx) {
                            exx.printStackTrace();
                        }

                    }

                }
            }
        });


    }
}

private void autoStartVivo(final Context context) {
    if (isPackageExists(context, PACKAGE_VIVO_MAIN) || isPackageExists(context, PACKAGE_VIVO_FALLBACK)) {
        showAlert(context, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                try {
                    PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                    startIntent(context, PACKAGE_VIVO_MAIN, PACKAGE_VIVO_COMPONENT);
                } catch (Exception e) {
                    e.printStackTrace();
                    try {
                        PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                        startIntent(context, PACKAGE_VIVO_FALLBACK, PACKAGE_VIVO_COMPONENT_FALLBACK);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        try {
                            PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                            startIntent(context, PACKAGE_VIVO_MAIN, PACKAGE_VIVO_COMPONENT_FALLBACK_A);
                        } catch (Exception exx) {
                            exx.printStackTrace();
                        }

                    }

                }

            }
        });
    }
}

private void autoStartNokia(final Context context) {
    if (isPackageExists(context, PACKAGE_NOKIA_MAIN)) {
        showAlert(context, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                try {
                    PrefUtil.writeBoolean(context, PrefUtil.PREF_KEY_APP_AUTO_START, true);
                    startIntent(context, PACKAGE_NOKIA_MAIN, PACKAGE_NOKIA_COMPONENT);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}


private void startIntent(Context context, String packageName, String componentName) throws Exception {
    try {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName, componentName));
        context.startActivity(intent);
    } catch (Exception var5) {
        var5.printStackTrace();
        throw var5;
    }
}

private Boolean isPackageExists(Context context, String targetPackage) {
    List<ApplicationInfo> packages;
    PackageManager pm = context.getPackageManager();
    packages = pm.getInstalledApplications(0);
    for (ApplicationInfo packageInfo :
            packages) {
        if (packageInfo.packageName.equals(targetPackage)) {
            return true;
        }
    }

    return false;
 }
}

在你的活动中

        AutoStartHelper.getInstance().getAutoStartPermission(this);

无法跟踪我们是否启用了自动启动。

【讨论】:

  • 在哪里可以找到PrefUtil
  • 你好,如果用户没有从设置中更改自动权限,但你保存 bolean 在共享首选项中的自动刷新中为 true .. 那么?
  • 无法跟踪用户是否已成功为您的应用程序启用自动启动。将 true 保存到首选项只是一种不要求用户自动启动它的方法,因此请在对话框提示中添加重要通知并告诉用户为什么此权限对您的应用程序如此重要,以及如果您的应用程序未授予此权限,用户将失去什么。
  • @Lasitha PrefUtil 只不过是您的 SharedPreferences
猜你喜欢
  • 2019-06-13
  • 2017-01-14
  • 2016-12-24
  • 2022-01-21
  • 1970-01-01
  • 2018-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多