如何以编程方式启用自动启动和浮动窗口权限

时间:2017-06-06 07:35:07

标签: android android-permissions autostart

如何以编程方式启用自动启动权限? 如何找到需要自动启动代码的手机? 如何检查自动启动权限是启用还是禁用?

我只能通过canDrawOverlay()权限查找显示弹出式权限。

请帮助我搜索了很多,如果没有启用我想为设备启用自动启动。我找到的一些解决方案如下所示

我找到了xiaomi,honor和letv的代码,但我想要联想同样的东西

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

            }

4 个答案:

答案 0 :(得分:9)

当你从playstore下载应用程序时,自动启动功能将自动启用,如果xiaomi OS需要它,因为像amazon,google IO等应用程序也不允许自动启动,在这种情况下你必须转到安全权限 - &gt ;自动启动 - >然后从那里启用自动启动。您无法通过代码使应用程序自动启动您可以执行的是您可以显示一个对话框以启用自动启动并将用户带到自动启动活动但这不是一个好的选项,因为您无法检查自动启动是否是启用与否。

这是由MI在MIUI8中为节省电池完成的。这个问题浪费了我的2天XD

参考

您可以参考文章MIUI8

答案 1 :(得分:7)

** Oppo和Vivo的以下两种解决方案可以解决您的问题,这对我有用**

  

请检查以下解决方案以启用浮动窗口权限   在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) {

                }
            }
        }
}

答案 2 :(得分:2)

正如上面其他人所说,无法确定是否已启用“自动启动”选项,但是我们可以使用一种意图将用户重定向到自动启动设置,因为这取决于用户是否允许还是不允许。

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

我已经在小米和OPPO电话上对此进行了测试,并且我相信这段代码也将适用于其他自定义UI设备,例如vivo等。

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

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

答案 3 :(得分:1)

使用此帮助程序类

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

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