native.js,安卓判断APP是否在电池优化白名单
配置权限
" <uses-permission android:name=\"android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS\"/>",
// 白名单
var main = plus.android.runtimeMainActivity();
var packName = main.getPackageName();
var Context = plus.android.importClass("android.content.Context");
var PowerManager = plus.android.importClass("android.os.PowerManager");
// 获取电源类
var pm = main.getSystemService(Context.POWER_SERVICE);
console.log('是否在白名单:',pm.isIgnoringBatteryOptimizations(packName)); //是否白名单
let whiteList = pm.isIgnoringBatteryOptimizations(packName);
调起服务
try{
var Uri = plus.android.importClass("android.net.Uri");
var Settings = plus.android.importClass("android.provider.Settings");
var packageURI = Uri.parse("package:" + packName);
var intents = plus.android.newObject("android.content.Intent", Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,packageURI); // 电池
main.startActivity(intents);
}catch{
// console.log('调起失败')
}
插件吊起
if(pm.isIgnoringBatteryOptimizations(packName)){
console.log(11)
}else{
uni.showModal({
title: '提示',
content: '请开启自启动权限和省电策略设置无限制!',
success: function (res) {
if (res.confirm) {
settings.openAppSetting();
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
}