How to programmatically enable auto start and floating window permissions

雨燕双飞 提交于 2019-12-27 11:59:12

问题


How can I enable auto start permission programmatically ? How to find which phone need to do auto-start code ? How to Check Auto start permission is enable or disable ?

I am able to find only about display popup permission with canDrawOverlay() permission`.

please help I have searched a lot ,I want to enable auto-start for device if it is not enabled.Some solutions I have found as below


I have found code for xiaomi,honor and letv but I want same thing for Lenovo

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

            }

回答1:


Please check the following solution to enable floating window and autostart permission for OPPO and VIVO mobiles.

On Oppo device

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

    }
}

Auto Start permission for 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();
            }
        }
    }
}

Auto start for 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:


The autostart feature will get enabled automatically when you will download the app from playstore if xiaomi OS wants it as apps like amazon ,google IO etc are also not allowed to autostart ,In this case you have to go to Security permissions -> autostart -> then enable autostart from there.You cannot make the app autostart by code all you can do is you can show a dialog to enable auto start and take the user to the autostart activity but this is not a good option as you cannot check whether autostart is enabled or not.

This is done by Mi in MIUI8 for saving battery .This issue wasted my 2 days XD

Reference

You can refer to the article MIUI8




回答3:


As said above by others that there's no way to find out whether the Auto-start option is enabled or not but we can use an intent to redirect the user to the auto start settings now it is up-to user to allow it or not to allow it.

We can use the ACTION_APPLICATION_DETAILS_SETTINGS or ACTION_MANAGE_APPLICATIONS_SETTINGS flags to redirect directly to the auto start settings screen.

I have tested this on Xiaomi & OPPO Phones and i believe that this code will also work for other custom UI devices like vivo etc.

On click of a popup dialogue which says please check and enable Auto start option from the app setting screen. Put the below code on click of the OK button.

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



回答4:


Use this helper class

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

in your activity

        AutoStartHelper.getInstance().getAutoStartPermission(this);

there is no way to track that we have enabled autostart or not.



来源:https://stackoverflow.com/questions/44383983/how-to-programmatically-enable-auto-start-and-floating-window-permissions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!