import Build from 'android.os.Build'
|
import Context from 'android.content.Context'
|
import Intent from 'android.content.Intent'
|
import Settings from 'android.provider.Settings'
|
import { UTSAndroid } from 'io.dcloud.uts'
|
import BatteryManager from 'android.os.BatteryManager'
|
|
// 拉起应用
|
export function openDialog(): void {
|
const context = UTSAndroid.getAppContext()
|
if (context == null) return
|
try {
|
const pm = context.getPackageManager()
|
const intent = pm.getLaunchIntentForPackage(context.getPackageName())
|
if (intent != null) {
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
context.startActivity(intent)
|
}
|
} catch (e) {
|
console.error(e)
|
}
|
}
|
|
// 跳到允许悬浮窗权限页面
|
export function allowFloat() {
|
const context = UTSAndroid.getAppContext()
|
if (context == null) return
|
// 1. 检查悬浮窗权限 (Android 6.0+)
|
const noneFloat = Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(context)
|
if (noneFloat) {
|
uni.showToast({ title: '请允许应用悬浮在屏幕上', icon: 'none' })
|
} else {
|
setTimeout(() => {
|
try {
|
// 无权限,跳转到权限设置页面
|
const intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
|
intent.setData(android.net.Uri.parse('package:' + context.getPackageName()))
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
context.startActivity(intent)
|
} catch (e) {
|
console.error(e)
|
}
|
}, 2000)
|
}
|
}
|
|
// 跳到后台弹出界面权限页面(应用详情设置)
|
export function allowBackgroundPopup() {
|
const context = UTSAndroid.getAppContext()
|
if (context == null) return
|
try {
|
const intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
|
intent.setData(android.net.Uri.parse('package:' + context.getPackageName()))
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
context.startActivity(intent)
|
} catch (e) {
|
console.error(e)
|
}
|
}
|
|
// 跳到锁屏显示权限页面(通知设置)
|
export function allowLockScreen() {
|
const context = UTSAndroid.getAppContext()
|
if (context == null) return
|
try {
|
if (Build.VERSION.SDK_INT >= 26) {
|
// Android 8.0+ 跳转到应用通知设置
|
const intent = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
|
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName())
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
context.startActivity(intent)
|
} else {
|
// 低版本跳转到应用详情
|
const intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
|
intent.setData(android.net.Uri.parse('package:' + context.getPackageName()))
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
context.startActivity(intent)
|
}
|
} catch (e) {
|
console.error(e)
|
}
|
}
|
|
/**
|
* 获取电池电量百分比
|
*
|
* uni-app项目中调用示例:
|
* import { getBatteryCapacity } from "@/uni_modules/lgh-getbatteryinfo"
|
* const capacity = getBatteryCapacity()
|
* console.log('电池电量:', capacity)
|
*/
|
export function getBatteryCapacity(): string {
|
// 获取android系统 application上下文
|
const context = UTSAndroid.getAppContext()
|
if (context != null) {
|
const manager = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager
|
const currentLevel: number = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
|
return '' + currentLevel + '%'
|
}
|
return '0%'
|
}
|