jsy-app/uni_modules/zy-mqtt/utssdk/app-android/service.uts

111 lines
4.5 KiB
Plaintext
Raw Normal View History

2024-12-19 12:34:26 +08:00
import { UTSAndroid } from "io.dcloud.uts";
import Activity from 'android.app.Activity';
import Bundle from 'android.os.Bundle';
import Service from 'android.app.Service';
import Intent from 'android.content.Intent';
import IBinder from 'android.os.IBinder';
import NotificationCompat from 'androidx.core.app.NotificationCompat';
import Build from 'android.os.Build';
import System from 'java.lang.System';
import NotificationManager from 'android.app.NotificationManager';
import NotificationChannel from 'android.app.NotificationChannel';
import Context from 'android.content.Context';
import Notification from 'android.app.Notification';
import PowerManager from 'android.os.PowerManager';
class ConnectedDeviceService extends Service {
static notificationContentText : string = '应用正在后台运行,这有助于提供定制的服务和改善用户体验。'
static notificationContentTitle : string = '后台应用持续运行'
wakeLock : PowerManager.WakeLock | null = null;
constructor() {
super();
}
override onCreate() : void {
const powerManager : PowerManager = UTSAndroid.getAppContext()!.getSystemService(Context.POWER_SERVICE) as PowerManager;
this.wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyApp:MyWakelockTag");
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//对于Android O及以上版本如果你的应用处于后台你需要将服务转变为前台服务来避免系统在一定时间后杀死服务。这需要通过创建一个持续在通知栏显示的通知来实现。
this.startMyOwnForeground();
} else {
this.startForeground(1, new Notification());
}
}
override onStartCommand(intent : Intent, flags : Int, startId : Int) : Int {
// 在这里执行后台任务
this.wakeLock?.acquire();
// 使服务在被杀死后自动重启
// return START_STICKY;
return START_STICKY
}
startMyOwnForeground() : void {
try {
const NOTIFICATION_CHANNEL_ID = "connectedDevice";
const channelName = "My Background connectedDevice Service";
const chan : NotificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_DEFAULT);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
const manager : NotificationManager = UTSAndroid.getAppContext()!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager;
manager.createNotificationChannel(chan);
const notificationBuilder : NotificationCompat.Builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
const notification : Notification = notificationBuilder.setOngoing(true)
.setContentText(ConnectedDeviceService.notificationContentText)
.setContentTitle(ConnectedDeviceService.notificationContentTitle)
.setSmallIcon(android.R.drawable.ic_menu_mylocation)
.setVisibility(Notification.VISIBILITY_PUBLIC) // 可以在锁屏时显示
.setPriority(NotificationManager.IMPORTANCE_DEFAULT)
.setCategory(Notification.CATEGORY_SERVICE)
.setWhen(System.currentTimeMillis())
.build();
this.startForeground(2, notification);
} catch (e) {
console.log(e)
//TODO handle the exception
}
}
override onBind(intent : Intent) : IBinder | null {
// 此例中不处理绑定服务的情况
return null;
}
override onDestroy() : void {
super.onDestroy();
// 清理资源,例如注销广播接收器或停止自定义线程
// 释放Wakelock锁
if (this.wakeLock!.isHeld()) {
this.wakeLock!.release();
this.wakeLock = null
}
}
}
// // 开启服务
export function startConnectedDeviceService(title ?: string, context ?: string) : void {
console.log('开启后台运行service')
if (title != null) {
ConnectedDeviceService.notificationContentTitle = title as string
}
if (context != null) {
ConnectedDeviceService.notificationContentText = context as string
}
// 获取实例如下所示,首次获取实例会做初始化操作,可能会有耗时,因此可以尽可能提前初始化时机。
let serviceIntent : Intent = new Intent(UTSAndroid.getUniActivity(), ConnectedDeviceService().javaClass);
// 设置连接配置
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
UTSAndroid.getAppContext()!.startForegroundService(serviceIntent);
} else {
UTSAndroid.getAppContext()!.startService(serviceIntent);
}
}
export function closeConnectedDeviceService() : void {
let serviceIntent : Intent = new Intent(UTSAndroid.getUniActivity(), ConnectedDeviceService().javaClass);
UTSAndroid.getAppContext()!.stopService(serviceIntent);
}