Ba-KeepAlive 是一款android原生保活插件,支持市面上大部分机型,Android4.4到Android13.0 ( 注意: 不保证支持所有机型和场景,建议先试用)。

在 script 中引入组件
const keepAlive = uni.requireNativePlugin('Ba-KeepAlive')
在 script 中调用
data() {
return {
channelId: 'Ba-KeepAlive',
channelName: "Ba-KeepAlive",
title: "Ba-KeepAlive",
content: "Ba-KeepAlive is running",
dataResult: "",
type: undefined
}
},
methods: {
register() { //注册
keepAlive.register({
channelId: this.channelId,
channelName: this.channelName,
title: this.title,
content: this.content,
},
(res) => {
console.log(res);
uni.showToast({
title: res.msg,
icon: "none",
duration: 3000
})
});
},
isRunning() { //是否正在运行
keepAlive.isRunning((res) => {
console.log(res);
uni.showToast({
title: res.msg,
icon: "none",
duration: 3000
})
});
},
restart() { //重启
keepAlive.restart((res) => {
console.log(res);
uni.showToast({
title: res.msg,
icon: "none",
duration: 3000
})
});
},
unregister() { //注销
keepAlive.unregister((res) => {
console.log(res);
uni.showToast({
title: res.msg,
icon: "none",
duration: 3000
})
});
},
whiteList() { //获取并打开白名单
keepAlive.whiteList({
isDialog: true,
reason: "轨迹服务",
whiteType: 0,
}, (res) => {
console.log(res);
uni.showToast({
title: res.msg,
icon: "none",
duration: 3000
})
});
},
openWhiteList() { //打开白名单
try {
var list = JSON.parse(this.dataResult);
keepAlive.openWhiteList({
isDialog: true,
reason: "轨迹服务",
type: list[0].type,
}, (res) => {
console.log(res);
uni.showToast({
title: res.msg,
icon: "none",
duration: 3000
})
});
} catch (e) {
uni.showToast({
title: "请先获取有效的白名单项",
icon: "none",
duration: 3000
})
}
},
getWhiteList() { //获取白名单项
keepAlive.getWhiteList({
whiteType: 0,
}, (res) => {
console.log(res);
this.dataResult = res.data ? JSON.stringify(res.data) : "";
uni.showToast({
title: res.msg,
icon: "none",
duration: 3000
})
});
},
}

| 名称 | 说明 |
|---|---|
| register | 注册保活服务,建议在Application里初始化 |
| isRunning | 查看保活服务是否运行 |
| restart | 重启保活服务,与register区别在于不会重新配置,而是使用上一次配置的信息 |
| unregister | 注销保活服务,并不会立马停止,而是在1s之后停止,非必须调用,比如可以在app完全退出的时候可以调用,根据你的需求调用 |
| whiteList | 获取并打开白名单设置项 |
| openWhiteList | 打开指定白名单设置项 |
| getWhiteList | 获取当前设备支持的白名单设置项 |
注册保活服务
| 属性名 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| channelId | String | 建议 | ‘Ba-KeepAlive’ | 渠道Id,建议用户修改,非必传 |
| channelName | String | 建议 | ‘Ba-KeepAlive’ | 渠道名,用于设置里通知渠道展示,建议用户修改,非必传 |
| title | String | 建议 | ‘Ba-KeepAlive’ | 通知栏标题,建议用户修改,非必传 |
| content | String | 建议 | ‘Ba-KeepAlive is running’ | 通知栏内容,建议用户修改,非必传 |
| showLargeIcon | Boolean | false | false | 是否展示通知栏大图标 |
| largeIcon | String | false | ‘’ | 通知栏大图标绝对路径,不填获取项目文件,见"UI 图标设置" |
| serviceId | Number | false | 随机数 | 服务Id,默认是1到Int.MAX_VALUE随机数,非必传 |
| workManager | Boolean | false | 是否可以使用WorkManager,默认可以使用,非必传 | |
| crashRestartUI | Boolean | false | 奔溃是否可以重启用户界面,默认为false,google原生rom android 10 以下可以正常重启,非必传 | |
| hideNotification | Boolean | false | 是否隐藏通知栏,经测试,除了android 7.1手机之外都可以隐藏,默认隐藏,非必传 | |
| hideNotificationAfterO | Boolean | false | 是否隐藏Android 8.0以上通知栏,默认隐藏 | |
| musicEnabled | Boolean | false | true | 是否可以播放音乐,默认可以播放音乐,非必传 |
| backgroundMusicEnabled | Boolean | false | false | 后台是否可以播放音乐,默认不可以后台播放音乐,非必传 |
| musicInterval | Number | false | 0 | 设置音乐间隔时间,时间间隔越长,越省电,默认间隔时间是0,非必传 |
| onePixEnabled | Boolean | false | true | 是否可以使用一像素,默认可以使用,只有在android p以下可以使用,非必传 |
获取并打开白名单设置项
| 属性名 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| isDialog | Boolean | false | true | 是否展示默认弹窗提示 |
| reason | String | false | ‘核心服务的持续运行’ | 默认弹窗原因内容 |
| whiteType | Number | false | 0 | 白名单类型过滤,0:所有 1:自启动 2:耗电管理 3:锁屏清理 4:神隐模式 5:应用保护 6:后台管理 7:锁屏加速受保护 99:Doze 模式 |
打开指定白名单设置项
| 属性名 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| type | Number | true | 指定的白名单类型,从getWhiteList方法获取 | |
| isDialog | Boolean | false | true | 是否展示默认弹窗提示 |
| reason | String | false | ‘核心服务的持续运行’ | 默认弹窗原因内容 |
获取当前设备支持的白名单设置项
| 属性名 | 类型 | 必填 | 默认值 | 说明 |
|---|---|---|---|---|
| whiteType | Number | false | 0 | 白名单类型过滤,0:所有 1:自启动 2:耗电管理 3:锁屏清理 4:神隐模式 5:应用保护 6:后台管理 7:锁屏加速受保护 99:Doze 模式 |
应用消息通知插件(多种样式,新增支持常驻通知模式) Ba-Notify(文档)
应用未读角标插件 Ba-Shortcut-Badge (文档)
扫码原生插件(毫秒级、支持多码)Ba-Scanner-G(文档)
扫码原生插件 - 新(可任意自定义界面版本;支持连续扫码;支持设置扫码格式)Ba-Scanner(文档)
动态修改状态栏、导航栏背景色、字体颜色插件 Ba-AppBar(文档)
安卓保活插件(采用多种主流技术) Ba-KeepAlive(文档)
安卓快捷方式(桌面长按app图标) Ba-Shortcut(文档)
自定义图片水印(任意位置) Ba-Watermark(文档)
最接近微信的图片压缩插件 Ba-ImageCompressor(文档)
视频压缩、视频剪辑插件 Ba-VideoCompressor(文档)
动态切换应用图标、名称(如新年、国庆等) Ba-ChangeIcon(文档)
原生Toast弹窗提示(穿透所有界面、穿透原生;自定义颜色、图标 ) Ba-Toast(文档)
websocket原生服务(自动重连、心跳检测) Ba-Websocket(文档)
智能安装(自动升级) Ba-SmartUpgrade(文档)
监听通知栏消息(支持白名单、黑名单、过滤) Ba-NotifyListener(文档)
全局置灰、哀悼置灰(可动态、同时支持nvue、vue) Ba-Gray(文档)
获取设备唯一标识(OAID、AAID、IMEI等) Ba-IdCode(文档)
实时定位(系统、后台运行、支持息屏)插件 Ba-Location(文档)
实时定位(高德、后台运行、支持息屏、坐标转换、距离计算) Ba-LocationAMap(文档)
窗口小工具、桌面小部件、微件 Ba-AppWidget(文档)
窗口小工具、桌面小部件、微件(日历、时间) Ba-AwCalendarS(文档)
悬浮窗(在其他应用上层显示) Ba-FloatWindow(文档)
悬浮窗(应用内、无需授权) Ba-FloatWindow2(文档)
悬浮窗(悬浮球、动态菜单、在其他应用上层显示) Ba-FloatBall(文档)