Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions app/src/main/java/cn/lliiooll/gmc/BotService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@
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();// 发个通知别让用户以为程序卡了
Gmc_android.setPluginPath(new File(getFilesDir(),"plugins").getAbsolutePath());
Gmc_android.setLogger(s -> {
// TODO print log on screen
});
new Thread(Gmc_android::start).start();
new Thread(Gmc_android::start).start();// 新建线程启动gmc
Toast.makeText(this, "Start Service", Toast.LENGTH_SHORT).show();
active = true;
startForeground(1, NotificationUtil.create(this));
startForeground(1, NotificationUtil.create(this));// 启动前台服务
super.onCreate();
}

Expand All @@ -46,7 +50,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
@Override
public void onDestroy() {
active = false;
stopForeground(true);
stopForeground(true);// 停止前台服务
super.onDestroy();
}

Expand Down
13 changes: 9 additions & 4 deletions app/src/main/java/cn/lliiooll/gmc/util/NotificationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import cn.lliiooll.gmc.BotService;
import net.lz1998.gomiraiclient.MainActivity;
import net.lz1998.gomiraiclient.R;

/**
* 通知工具类
*/
public class NotificationUtil {

/**
* 创建一个用于挂前台服务的通知
* @param service 服务
* @return 通知
*/
public static Notification create(BotService service) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "GMC";
Expand Down
24 changes: 23 additions & 1 deletion app/src/main/java/cn/lliiooll/gmc/util/PermissionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,32 @@
import android.os.Build;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import net.lz1998.gomiraiclient.MainActivity;
import org.jetbrains.annotations.NotNull;

/**
* 权限类
*/
public class PermissionUtil {
/**
* 所有程序用到的权限
*/
private static final String[] permissions = new String[]{
Manifest.permission.MANAGE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.FOREGROUND_SERVICE,
Manifest.permission.INTERNET,
};
/**
* 权限请求id
*/
private static final int requestCode = 0x9081;

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

/**
* 请求权限
* @param activity 界面
*/
public static void request(Activity activity) {
ActivityCompat.requestPermissions(activity, permissions, requestCode);
}

/**
* 判断是不是程序的requestCode
* @param requestCode
* @return
*/
public static boolean isSelfRequest(int requestCode) {
return requestCode == PermissionUtil.requestCode;
}
Expand Down
7 changes: 2 additions & 5 deletions app/src/main/java/net/lz1998/gomiraiclient/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ class MainActivity : AppCompatActivity() {
}
// 启动服务
if (!BotService.isActive()) {
Toast.makeText(this, "Start", Toast.LENGTH_SHORT).show();
BotService.start(this)
}
val t = this;
thread {
Thread.sleep(3000L)
t.runOnUiThread {
Thread.sleep(3000L)// 等3秒让gmc启动
t.runOnUiThread {// UI线程更新界面
binding.webview.settings.javaScriptEnabled = true
binding.webview.settings.loadWithOverviewMode = true
binding.webview.webViewClient = object : WebViewClient() {
Expand All @@ -53,14 +52,12 @@ class MainActivity : AppCompatActivity() {
) {
// Permission is granted. Continue the action or workflow
// in your app.
Toast.makeText(this, "权限通过", Toast.LENGTH_LONG).show()
} else {
// Explain to the user that the feature is unavailable because
// the features requires a permission that the user has denied.
// At the same time, respect the user's decision. Don't link to
// system settings in an effort to convince the user to change
// their decision.
Toast.makeText(this, "权限不通过", Toast.LENGTH_LONG).show()
}


Expand Down