Skip to content

Commit 41fe4eb

Browse files
authored
Merge pull request #2 from lliioollcn/main
添加注释
2 parents ad705b1 + 624b327 commit 41fe4eb

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

app/src/main/java/cn/lliiooll/gmc/BotService.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,25 @@
1414
import net.lz1998.gomiraiclient.MainActivity;
1515
import org.jetbrains.annotations.NotNull;
1616

17+
/**
18+
* 用来运行gmc的服务
19+
*/
1720
import java.io.File;
1821

1922
public class BotService extends Service {
2023
private static boolean active = false;
2124

2225
@Override
2326
public void onCreate() {
27+
Toast.makeText(this, "GMC启动中...", Toast.LENGTH_LONG).show();// 发个通知别让用户以为程序卡了
2428
Gmc_android.setPluginPath(new File(getFilesDir(),"plugins").getAbsolutePath());
2529
Gmc_android.setLogger(s -> {
2630
// TODO print log on screen
2731
});
28-
new Thread(Gmc_android::start).start();
32+
new Thread(Gmc_android::start).start();// 新建线程启动gmc
2933
Toast.makeText(this, "Start Service", Toast.LENGTH_SHORT).show();
3034
active = true;
31-
startForeground(1, NotificationUtil.create(this));
35+
startForeground(1, NotificationUtil.create(this));// 启动前台服务
3236
super.onCreate();
3337
}
3438

@@ -46,7 +50,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
4650
@Override
4751
public void onDestroy() {
4852
active = false;
49-
stopForeground(true);
53+
stopForeground(true);// 停止前台服务
5054
super.onDestroy();
5155
}
5256

app/src/main/java/cn/lliiooll/gmc/util/NotificationUtil.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,22 @@
44
import android.app.NotificationChannel;
55
import android.app.NotificationManager;
66
import android.app.PendingIntent;
7-
import android.content.Context;
87
import android.content.Intent;
9-
import android.graphics.Color;
108
import android.os.Build;
119
import androidx.core.app.NotificationCompat;
12-
import androidx.core.app.NotificationManagerCompat;
1310
import cn.lliiooll.gmc.BotService;
14-
import net.lz1998.gomiraiclient.MainActivity;
1511
import net.lz1998.gomiraiclient.R;
1612

13+
/**
14+
* 通知工具类
15+
*/
1716
public class NotificationUtil {
17+
18+
/**
19+
* 创建一个用于挂前台服务的通知
20+
* @param service 服务
21+
* @return 通知
22+
*/
1823
public static Notification create(BotService service) {
1924
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
2025
CharSequence name = "GMC";

app/src/main/java/cn/lliiooll/gmc/util/PermissionUtil.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,32 @@
66
import android.os.Build;
77
import androidx.core.app.ActivityCompat;
88
import androidx.core.content.ContextCompat;
9-
import net.lz1998.gomiraiclient.MainActivity;
109
import org.jetbrains.annotations.NotNull;
1110

11+
/**
12+
* 权限类
13+
*/
1214
public class PermissionUtil {
15+
/**
16+
* 所有程序用到的权限
17+
*/
1318
private static final String[] permissions = new String[]{
1419
Manifest.permission.MANAGE_EXTERNAL_STORAGE,
1520
Manifest.permission.READ_EXTERNAL_STORAGE,
1621
Manifest.permission.WRITE_EXTERNAL_STORAGE,
1722
Manifest.permission.FOREGROUND_SERVICE,
1823
Manifest.permission.INTERNET,
1924
};
25+
/**
26+
* 权限请求id
27+
*/
2028
private static final int requestCode = 0x9081;
2129

30+
/**
31+
* 检查程序是否有需要的权限
32+
* @param activity 界面
33+
* @return 是否拥有全部权限
34+
*/
2235
@NotNull
2336
public static boolean checkAll(Activity activity) {
2437
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
@@ -33,10 +46,19 @@ public static boolean checkAll(Activity activity) {
3346
return true;
3447
}
3548

49+
/**
50+
* 请求权限
51+
* @param activity 界面
52+
*/
3653
public static void request(Activity activity) {
3754
ActivityCompat.requestPermissions(activity, permissions, requestCode);
3855
}
3956

57+
/**
58+
* 判断是不是程序的requestCode
59+
* @param requestCode
60+
* @return
61+
*/
4062
public static boolean isSelfRequest(int requestCode) {
4163
return requestCode == PermissionUtil.requestCode;
4264
}

app/src/main/java/net/lz1998/gomiraiclient/MainActivity.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ class MainActivity : AppCompatActivity() {
2424
}
2525
// 启动服务
2626
if (!BotService.isActive()) {
27-
Toast.makeText(this, "Start", Toast.LENGTH_SHORT).show();
2827
BotService.start(this)
2928
}
3029
val t = this;
3130
thread {
32-
Thread.sleep(3000L)
33-
t.runOnUiThread {
31+
Thread.sleep(3000L)// 等3秒让gmc启动
32+
t.runOnUiThread {// UI线程更新界面
3433
binding.webview.settings.javaScriptEnabled = true
3534
binding.webview.settings.loadWithOverviewMode = true
3635
binding.webview.webViewClient = object : WebViewClient() {
@@ -53,14 +52,12 @@ class MainActivity : AppCompatActivity() {
5352
) {
5453
// Permission is granted. Continue the action or workflow
5554
// in your app.
56-
Toast.makeText(this, "权限通过", Toast.LENGTH_LONG).show()
5755
} else {
5856
// Explain to the user that the feature is unavailable because
5957
// the features requires a permission that the user has denied.
6058
// At the same time, respect the user's decision. Don't link to
6159
// system settings in an effort to convince the user to change
6260
// their decision.
63-
Toast.makeText(this, "权限不通过", Toast.LENGTH_LONG).show()
6461
}
6562

6663

0 commit comments

Comments
 (0)