package com.hdl.photovoltaic.uni;
|
|
import android.app.ActivityManager;
|
import android.app.Notification;
|
import android.app.NotificationChannel;
|
import android.app.NotificationManager;
|
import android.app.Service;
|
import android.content.Context;
|
import android.content.Intent;
|
import android.os.Build;
|
import android.os.IBinder;
|
import android.util.Log;
|
|
import androidx.annotation.Nullable;
|
import androidx.core.app.NotificationCompat;
|
|
import com.hdl.photovoltaic.R;
|
|
import java.util.List;
|
|
|
public class MyForegroundService extends Service {
|
private static final String CHANNEL_ID = "MyForegroundServiceChannel";
|
private static final int NOTIFICATION_ID = 1;
|
|
private static boolean isRunning = false;
|
|
@Override
|
public void onCreate() {
|
super.onCreate();
|
// 创建通知渠道(Android 8.0+)
|
createNotificationChannel();
|
isRunning = true;
|
|
}
|
@Override
|
public void onTaskRemoved(Intent rootIntent) {
|
super.onTaskRemoved(rootIntent);
|
// 当应用从最近任务中移除时调用
|
stopForeground(true);
|
stopSelf();
|
killAppProcess(this);
|
// // 3. 结束进程
|
// android.os.Process.killProcess(android.os.Process.myPid());
|
// System.exit(0);
|
}
|
@Override
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
// 创建通知
|
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
|
.setContentTitle("")//宿主进程服务
|
.setContentText("")//正在与uni-app保持通信
|
.setSmallIcon(R.drawable.notification_logo) // 必须设置一个图标
|
.setPriority(NotificationCompat.PRIORITY_LOW)
|
// .setOngoing(true) // 设置通知为持续通知,不可清除
|
// .setAutoCancel(false) // ← 设置为不可自动取消
|
// .setShowWhen(false) // 不显示时间
|
// .setOnlyAlertOnce(true) // 只提示一次
|
.build();
|
|
// 启动前台服务
|
startForeground(NOTIFICATION_ID, notification);
|
|
// 如果服务被杀死,不再重新创建(重建:START_STICKY,不重建:START_NOT_STICKY)
|
return START_NOT_STICKY;
|
}
|
|
private void createNotificationChannel() {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
NotificationChannel serviceChannel = new NotificationChannel(
|
CHANNEL_ID,
|
getString(R.string.host_process_service_channel),
|
NotificationManager.IMPORTANCE_LOW // 设置为低重要性,不会发出声音
|
);
|
// serviceChannel.setDescription("用于保持宿主进程运行");
|
NotificationManager manager = getSystemService(NotificationManager.class);
|
manager.createNotificationChannel(serviceChannel);
|
}
|
}
|
|
@Nullable
|
@Override
|
public IBinder onBind(Intent intent) {
|
return null;
|
}
|
|
@Override
|
public void onDestroy() {
|
super.onDestroy();
|
isRunning = false;
|
// 清理资源
|
stopForeground(true);
|
}
|
|
public static boolean isServiceRunning() {
|
return isRunning;
|
}
|
|
/**
|
* 关掉app所有进程
|
*
|
* @param context 上下文
|
*/
|
private void killAppProcess(Context context) {
|
// AppManagerUtils.getAppManager().finishAllActivity();
|
// Log.d("===6666666","");
|
try {
|
android.app.ActivityManager manager = (android.app.ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
List<ActivityManager.RunningAppProcessInfo> processInfos = manager.getRunningAppProcesses();
|
// 先杀掉相关进程,最后再杀掉主进程
|
for (android.app.ActivityManager.RunningAppProcessInfo runningAppProcessInfo : processInfos) {
|
if ((runningAppProcessInfo.uid == android.os.Process.myUid()) && (runningAppProcessInfo.pid != android.os.Process.myPid())) {
|
try {
|
android.os.Process.killProcess(runningAppProcessInfo.pid);
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
android.os.Process.killProcess(android.os.Process.myPid());
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
System.exit(0);
|
}
|
}
|