更新 MainActivity.java
parent
e59de5764e
commit
cc4ef4939c
|
|
@ -11,8 +11,10 @@ import androidx.appcompat.app.AppCompatActivity;
|
|||
|
||||
import com.nbee.echolink.async.BatteryOptimizationTask;
|
||||
import com.nbee.echolink.service.MonitorService;
|
||||
import com.nbee.echolink.service.NotificationListener;
|
||||
import com.nbee.echolink.utils.BatteryOptimizationUtil;
|
||||
import com.nbee.echolink.utils.PermissionUtils;
|
||||
import com.nbee.echolink.utils.SharedPreferencesManager;
|
||||
import com.nbee.echolink.utils.ShellUtils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
|
|
@ -25,6 +27,7 @@ import timber.log.Timber;
|
|||
public class MainActivity extends AppCompatActivity {
|
||||
private final String TAG = "MainActivity";
|
||||
private PermissionUtils permissionUtils;
|
||||
private SharedPreferencesManager spManager;
|
||||
private TextView logTextView; // 将类型从 View 更改为 TextView
|
||||
|
||||
@Override
|
||||
|
|
@ -35,14 +38,31 @@ public class MainActivity extends AppCompatActivity {
|
|||
logTextView = findViewById(R.id.logTextView);
|
||||
updateLog("初始化日志...");
|
||||
|
||||
spManager = new SharedPreferencesManager(this);
|
||||
|
||||
if (isFirstRun()) {
|
||||
setupInitialDataAsync();
|
||||
spManager.putBoolean("isFirstRun", false);
|
||||
}
|
||||
|
||||
requestIgnoreBatteryAsync();
|
||||
permissionUtils = new PermissionUtils(this);
|
||||
permissionUtils.checkPermissions();
|
||||
if (permissionUtils.isNotificationServiceEnabled(this)) {
|
||||
// 拥有权限,可以执行相关操作
|
||||
} else {
|
||||
// 未获得权限,可能需要提示用户
|
||||
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
// 启动MonitorService服务
|
||||
|
||||
Intent serviceIntent = new Intent(this, MonitorService.class);
|
||||
startService(serviceIntent);
|
||||
|
||||
//启动通知监听服务
|
||||
Intent serviceIntent1 = new Intent(this, NotificationListener.class);
|
||||
startService(serviceIntent1);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -57,6 +77,20 @@ public class MainActivity extends AppCompatActivity {
|
|||
readLogFileAsync();
|
||||
}
|
||||
|
||||
private boolean isFirstRun() {
|
||||
// 默认值为 true,如果找不到该键值对
|
||||
return spManager.getBoolean("isFirstRun", true);
|
||||
}
|
||||
//设置通知监控的包名
|
||||
private void setupInitialDataAsync() {
|
||||
new Thread(() -> {
|
||||
// 在这里设置SharedPreferences中的包名和对应的应用名
|
||||
// 例如:
|
||||
spManager.putString("com.tencent.mm", "微信");
|
||||
Timber.d("写入SharedPreferences成功。");
|
||||
// 完成后,您可以在这里执行其他操作,如发送广播、更新UI等(确保在主线程中执行UI操作)
|
||||
}).start();
|
||||
}
|
||||
|
||||
private void readLogFileAsync() {
|
||||
new AsyncTask<Void, Void, String>() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue