Android中添加APP到白名单

参考网址:https://blog.csdn.net/xiaoerbuyu1233/article/details/122130165

在 AndroidManifest.xml 文件中配置一下权限

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

加入白名单工具类

package com.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.PowerManager;
import android.provider.Settings;
import android.support.annotation.RequiresApi;

public class AlwaysLive {
    final static String  IS_HUAWEI = "isHuawei"; //华为
    final static String  IS_XIAOMI = "isXiaomi"; //小米
    final static String  IS_OPPO = "isOppo";  //oppo
    final static String  IS_VIVO = "isVivo"; //vivo
    final static String  IS_MEIZU = "isMeizu"; //魅族
    final static String  IS_SAMSUNG = "isSamsung"; //三星
    final static String  IS_LETV = "isLetv"; //乐视
    final static String  IS_SMARTISAN = "isSmartisan"; //锤子

    //判断应用是否添加在白名单之中
    @RequiresApi(Build.VERSION_CODES.M)
    public static boolean isIgnoringBatteryOptimizations(Context context){
        boolean isIgnoring = false;
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

        if (powerManager != null)
            isIgnoring = powerManager.isIgnoringBatteryOptimizations(context.getPackageName());

        return isIgnoring;
    }
    //如果不存在则申请加入白名单,使用弹框引导用户
    public static void requestIgnoreBatteryOptimizations(Context 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();
        }
    }

    /**
     * 跳转到指定应用的首页
     */
    public static void showActivity(String packageName,Context context){
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        context.startActivity(intent);
    }
    /**
     *  跳转到指定应用的指定页面
     * */
    public static void showActivity(String packageName,String activityDir,Context context){
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName, activityDir));
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    //判断手机厂商
    public static String checkPhoneFirm(){
        String phoneState = Build.BRAND.toLowerCase(); //获取手机厂商
        if (phoneState.equals("huawei") || phoneState.equals("honor"))
            return IS_HUAWEI;
        else if (phoneState.equals("xiaomi") && Build.BRAND != null)
            return IS_XIAOMI;
        else if (phoneState.equals("oppo") && Build.BRAND != null)
            return IS_OPPO;
        else if (phoneState.equals("vivo") && Build.BRAND != null)
            return IS_VIVO;
        else if (phoneState.equals("meizu") && Build.BRAND != null)
            return IS_MEIZU;
        else if (phoneState.equals("samsung") && Build.BRAND != null)
            return IS_SAMSUNG;
        else if (phoneState.equals("letv") && Build.BRAND != null)
            return IS_LETV;
        else if (phoneState.equals("smartisan") && Build.BRAND != null)
            return IS_SMARTISAN;

        return "";
    }
    //前往设置管理
    public static void  gotoWhiteListSetting(Context context){
        if (checkPhoneFirm().equals(IS_HUAWEI)){
            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);
            }
        }else if (checkPhoneFirm().equals(IS_XIAOMI)){
            showActivity("com.miui.securitycenter",
                    "com.miui.permcenter.autostart.AutoStartManagementActivity",context);
        }else if (checkPhoneFirm().equals(IS_OPPO)){
            //oppo:操作步骤:权限隐私 -> 自启动管理 -> 允许应用自启动
            try {
                showActivity("com.coloros.phonemanager",context);
            } catch (Exception e) {
                try {
                    showActivity("com.oppo.safe",context);
                } catch (Exception e2) {
                    try {
                        showActivity("com.coloros.oppoguardelf", context);
                    } catch (Exception e3) {
                        showActivity("com.coloros.safecenter", context);
                    }
                }
            }
        }else if (checkPhoneFirm().equals(IS_VIVO)){
            //vivo:操作步骤:权限管理 -> 自启动 -> 允许应用自启动
            showActivity("com.iqoo.secure", context);
        }else if (checkPhoneFirm().equals(IS_MEIZU)){
            //魅族:操作步骤:权限管理 -> 后台管理 -> 点击应用 -> 允许后台运行
            showActivity("com.meizu.safe", context);
        }else if (checkPhoneFirm().equals(IS_SAMSUNG)){
            //三星:操作步骤:自动运行应用程序 -> 打开应用开关 -> 电池管理 -> 未监视的应用程序 -> 添加应用
            try {
                showActivity("com.samsung.android.sm_cn",context);
            } catch (Exception e) {
                showActivity("com.samsung.android.sm",context);
            }
        }else if (checkPhoneFirm().equals(IS_LETV)){
            //乐视:操作步骤:自启动管理 -> 允许应用自启动
            showActivity("com.letv.android.letvsafe","com.letv.android.letvsafe.AutobootManageActivity", context);
        }else if (checkPhoneFirm().equals(IS_SMARTISAN)){
            //锤子:操作步骤:权限管理 -> 自启动权限管理 -> 点击应用 -> 允许被系统启动
            showActivity("com.smartisanos.security", context );
        }
    }

}

使用

if (!AlwaysLive.isIgnoringBatteryOptimizations(this)){
    AlwaysLive.requestIgnoreBatteryOptimizations(this);
 }

使用2:通过测试发现以下方法不能使用,会出现崩溃现象

if (!AlwaysLive.isIgnoringBatteryOptimizations(this))
    AlwaysLive.gotoWhiteListSetting(this);
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 10.0引入了一项新功能,即应用程序安装名单。这个功能可以提供更加精确的应用程序安装控制,使用户能够自定义哪些应用程序有权限安装。下面是关于Android 10.0应用程序安装名单的一些重要信息。 首先,安装名单功能可以在“设置”应用程序找到。在设置,用户可以找到“应用程序和通知”选项,并在其找到“高级”选项。在“高级”选项,用户可以选择“特殊应用访问”选项,并在其找到“应用程序安装”选项。 在“应用程序安装”选项,用户可以看到当前安装名单包含的应用程序。如果用户想要将某个应用程序添加名单以允许其安装,只需点击“添加应用程序”按钮并选择要添加的应用程序即可。 另外,还有一些其他选项可供用户设置。例如,用户可以选择在安装过程显示一个提示对话框,以便进一步确认是否允许特定应用程序的安装。这个对话框将要求用户确认应用程序的安装权限,并要求用户输入密码、指纹或其他身份验证信息。 在安装名单,用户还可以选择是否允许安装未知来源的应用程序。未知来源的应用程序是指从除了Google Play商店之外的其他地方下载的应用程序。如果用户打开了“允许来自此源的应用程序”选项,则可以安装未知来源的应用程序。 总之,Android 10.0的应用程序安装名单功能为用户提供了更多的应用程序安装控制。通过在设置名单进行管理,用户可以选择允许哪些应用程序安装,并设置额外的安装确认要求。这个功能能够提高用户的安全性和隐私保护。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值