wjc
昨天 e7e48e1a65fdaa361e14e7e77e24043a63126bd6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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);
    }
}