引导用户加白名单工具类

public class KeepAliveUtil {


    private static final String TAG = "KeepAliveUtil";

    /**
     * 判断应用是否在白名单内
     *
     * @param context
     * @return
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    private static boolean isIgnoringBatteryOptimizations(Context context) {
        boolean isIgnoring = false;
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        if (powerManager != null) {
            isIgnoring = powerManager.isIgnoringBatteryOptimizations(context.getPackageName());
        }
        Log.d(TAG, "isIgnoringBatteryOptimizations: " + isIgnoring);
        return isIgnoring;
    }

    /**
     * 若应用不在白名单内 申请白名单权限
     *
     * @param context
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    public static void requestBatteryOptimizations(Context context) {
        if (!isIgnoringBatteryOptimizations(context)) {
            try {
                Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                intent.setData(Uri.parse("package:" + context.getPackageName()));
                context.startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 跳转到指定应用的首页
     */
    private static void showActivity(@NonNull String packageName, Context context) {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        context.startActivity(intent);
    }

    /**
     * 跳转到指定应用的指定页面
     */
    private static void showActivity(@NonNull String packageName, @NonNull String activityDir, Context context) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName, activityDir));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 是否是华为手机
     *
     * @return
     */
    private static boolean isHuawei() {
        if (Build.BRAND == null) {
            return false;
        } else {
            return Build.BRAND.toLowerCase().equals("huawei") || Build.BRAND.toLowerCase().equals("honor");
        }
    }

    /**
     * 跳转到华为设置中
     *
     * @param context
     */
    private static void goHuaweiSetting(Context context) {
        try {
            showActivity("com.huawei.systemmanager",
                    "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity", context);
        } catch (Exception e) {
            showActivity("com.huawei.systemmanager",
                    "com.huawei.systemmanager.optimize.bootstart.BootStartActivity", context);
        }
    }

    /**
     * 是否是oppo手机
     *
     * @return
     */
    private static boolean isOPPO() {
        return Build.BRAND != null && Build.BRAND.toLowerCase().equals("oppo");
    }

    /**
     * 跳转到oppo设置中
     *
     * @param context
     */
    private static void goOPPOSetting(Context context) {
        try {
            showActivity("com.coloros.phonemanager", context);
        } catch (Exception e1) {
            try {
                showActivity("com.oppo.safe", context);
            } catch (Exception e2) {
                try {
                    showActivity("com.coloros.oppoguardelf", context);
                } catch (Exception e3) {
                    showActivity("com.coloros.safecenter", context);
                }
            }
        }
    }

    /**
     * 是否是小米手机
     *
     * @return
     */
    public static boolean isXiaomi() {
        return Build.BRAND != null && Build.BRAND.toLowerCase().equals("xiaomi");
    }

    /**
     * 跳转到小米手机设置
     *
     * @param context
     */
    private static void goXiaomiSetting(Context context) {
        showActivity("com.miui.securitycenter",
                "com.miui.permcenter.autostart.AutoStartManagementActivity", context);
    }


    /**
     * 是否是vivo手机
     *
     * @return
     */
    private static boolean isVIVO() {
        return Build.BRAND != null && Build.BRAND.toLowerCase().equals("vivo");
    }

    /**
     * 跳转到vivo手机管家
     *
     * @param context
     */
    private static void goVIVOSetting(Context context) {
        showActivity("com.iqoo.secure", context);
    }


    /**
     * 是否是魅族手机
     *
     * @return
     */
    private static boolean isMeizu() {
        return Build.BRAND != null && Build.BRAND.toLowerCase().equals("meizu");
    }

    /**
     * 跳转到魅族设置
     *
     * @param context
     */
    private static void goMeizuSetting(Context context) {
        showActivity("com.meizu.safe", context);
    }

    /**
     * 是否是三星手机
     *
     * @return
     */
    public static boolean isSamsung() {
        return Build.BRAND != null && Build.BRAND.toLowerCase().equals("samsung");
    }

    /**
     * 跳转到三星设置
     *
     * @param context
     */
    private static void goSamsungSetting(Context context) {
        try {
            showActivity("com.samsung.android.sm_cn", context);
        } catch (Exception e) {
            showActivity("com.samsung.android.sm", context);
        }
    }

    /**
     * 是否是乐视手机
     *
     * @return
     */
    private static boolean isLeTV() {
        return Build.BRAND != null && Build.BRAND.toLowerCase().equals("letv");
    }

    /**
     * 跳转到乐视设置
     *
     * @param context
     */
    private static void goLetvSetting(Context context) {
        showActivity("com.letv.android.letvsafe",
                "com.letv.android.letvsafe.AutobootManageActivity", context);
    }

    /**
     * 是否是锤子手机
     *
     * @return
     */
    private static boolean isSmartisan() {
        return Build.BRAND != null && Build.BRAND.toLowerCase().equals("smartisan");
    }

    /**
     * 跳转到锤子设置
     *
     * @param context
     */
    private static void goSmartisanSetting(Context context) {
        showActivity("com.smartisanos.security", context);
    }

    /**
     * 打开手机设置
     * 调用此方法即可
     *
     * @param context
     */
    public static void openPhoneGuardian(Context context) {
        if (isOPPO()) {
            Log.d(TAG, "openPhoneGuardian: is oppo");
            //oppo手机
            goOPPOSetting(context);
        } else if (isHuawei()) {
            //华为手机
            goHuaweiSetting(context);
        } else if (isXiaomi()) {
            //小米手机
            goXiaomiSetting(context);
        } else if (isVIVO()) {
            //vivo手机
            goVIVOSetting(context);
        } else if (isMeizu()) {
            //魅族手机
            goMeizuSetting(context);
        } else if (isSamsung()) {
            //三星
            goSamsungSetting(context);
        } else if (isSmartisan()) {
            //锤子
            goSmartisanSetting(context);
        } else if (isLeTV()) {
            //乐视
            goLetvSetting(context);
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值