-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBotService.java
More file actions
88 lines (72 loc) · 2.42 KB
/
BotService.java
File metadata and controls
88 lines (72 loc) · 2.42 KB
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
package cn.lliiooll.gmc;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.widget.Toast;
import androidx.annotation.Nullable;
import cn.lliiooll.gmc.util.NotificationUtil;
import gmc_android.Gmc_android;
import net.lz1998.gomiraiclient.MainActivity;
import org.jetbrains.annotations.NotNull;
/**
* 用来运行gmc的服务
*/
import java.io.File;
public class BotService extends Service {
private static boolean active = false;
@Override
public void onCreate() {
Toast.makeText(this, "GMC启动中...", Toast.LENGTH_LONG).show();// 发个通知别让用户以为程序卡了
File gmcPath = new File(getFilesDir(), "gmc");
if (!gmcPath.exists()) {
gmcPath.mkdirs();
}
Gmc_android.chdir(gmcPath.getAbsolutePath()); // 修改golang工作目录
Gmc_android.setLogger(s -> {
// TODO print log on screen
});
new Thread(Gmc_android::start).start();// 新建线程启动gmc
Toast.makeText(this, "Start Service", Toast.LENGTH_SHORT).show();
active = true;
startForeground(1, NotificationUtil.create(this));// 启动前台服务
super.onCreate();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
active = false;
stopForeground(true);// 停止前台服务
super.onDestroy();
}
@NotNull
public static boolean isActive() {
return BotService.active;
}
public static void start(@NotNull MainActivity activity) {
Intent intent = new Intent(activity, BotService.class);
activity.bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}, BIND_AUTO_CREATE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
activity.startForegroundService(intent);
} else {
activity.startService(intent);
}
}
}