Update MainActivity.java

dev
wangsiyuan 2024-04-22 11:06:14 +08:00
parent d500f8ee41
commit 7681db0596
1 changed files with 101 additions and 17 deletions

View File

@ -29,34 +29,51 @@ import java.io.PrintWriter;
import timber.log.Timber;
public class MainActivity extends AppCompatActivity {
private final String TAG = "MainActivity";
private final String TAG = "MainActivity";
private PermissionUtils permissionUtils;
private SharedPreferencesManager spManager;
private TextView logTextView; // 将类型从 View 更改为 TextView
/**
*
*
*
* @param savedInstanceState null
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置对应的布局文件
setContentView(R.layout.activity_main);
// 初始化“清除日志”按钮
Button clearAllLogs = findViewById(R.id.clearLogsButton);
// 初始化日志文本视图并显示初始化信息
logTextView = findViewById(R.id.logTextView);
updateLog("初始化日志...");
// 初始化SharedPreferences管理器
spManager = new SharedPreferencesManager(this);
// 检查是否是首次运行,是则进行初始数据设置
if (isFirstRun()) {
setupInitialDataAsync();
spManager.putBoolean("isFirstRun", false);
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);
}
@ -65,10 +82,11 @@ public class MainActivity extends AppCompatActivity {
Intent serviceIntent = new Intent(this, MonitorService.class);
startService(serviceIntent);
//启动通知监听服务
// 启动通知监听服务
Intent serviceIntent1 = new Intent(this, NotificationListener.class);
startService(serviceIntent1);
// 设置点击“清除日志”按钮的监听器
clearAllLogs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -77,9 +95,19 @@ public class MainActivity extends AppCompatActivity {
});
}
/**
*
*
*
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// 向PermissionUtils传递权限请求的结果进行进一步处理。
permissionUtils.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@ -89,43 +117,87 @@ public class MainActivity extends AppCompatActivity {
readLogFileAsync();
}
/**
*
* <p>SharedPreferencesisFirstRuntrue</p>
*
* @return boolean truefalse
*/
private boolean isFirstRun() {
// 默认值为 true如果找不到该键值对
// 从SharedPreferences中获取“isFirstRun”键对应的值如果不存在则默认返回true
return spManager.getBoolean("isFirstRun", true);
}
//设置通知监控的包名
/**
*
* 线SharedPreferences
*
* 线广UI
* UI线
*/
private void setupInitialDataAsync() {
new Thread(() -> {
// 在这里设置SharedPreferences中的包名和对应的应用名
// 例如:
// 设置SharedPreferences中的包名和应用名示例
spManager.putString("com.tencent.mm", "微信");
// 日志输出表示SharedPreferences设置成功
Timber.d("写入SharedPreferences成功。");
// 完成后您可以在这里执行其他操作如发送广播、更新UI等确保在主线程中执行UI操作
// 在这里可以执行其他操作如需要更新UI请确保在主线程中进行
}).start();
}
/**
*
* 使AsyncTask
*
*
*/
private void readLogFileAsync() {
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... voids) {
// 在后台执行任务,读取日志文件内容
return readLogFile();
}
@Override
protected void onPostExecute(String logContent) {
// 在UI线程上执行将读取到的日志内容更新到UI
updateLog(logContent);
}
}.execute();
}
/**
*
*
* BatteryOptimizationTask
*
*/
private void requestIgnoreBatteryAsync() {
// 创建并执行BatteryOptimizationTask异步任务
new BatteryOptimizationTask(this).execute();
}
/**
*
*
* @param newLog
*
*/
private void updateLog(String newLog) {
logTextView.setText(newLog);
logTextView.setText(newLog); // 更新日志文本视图的内容为新的日志字符串
}
/**
*
* "echoLink.log"
*
*
*
*/
private void clearLogs() {
File logFile = new File(getFilesDir(), "logs/echoLink.log");
if (logFile.exists()) {
@ -134,30 +206,42 @@ public class MainActivity extends AppCompatActivity {
PrintWriter writer = new PrintWriter(logFile);
writer.print("");
writer.close();
updateLog("日志已清空。"); // 更新 TextView 显示日志已被清空
updateLog("日志已清空。"); // 更新日志状态,提示日志已清空
} catch (FileNotFoundException e) {
e.printStackTrace();
updateLog("清空日志文件时出错。");
updateLog("清空日志文件时出错。"); // 更新日志状态,提示清空日志文件时出现错误
}
} else {
updateLog("日志文件不存在,无需清空。");
updateLog("日志文件不存在,无需清空。"); // 更新日志状态,表示无需清空日志文件
}
}
/**
*
* <p>"echoLink.log"</p>
* <p>"日志文件不存在"
* IO"读取日志文件时出错"</p>
*
* @return
*/
private String readLogFile() {
try {
File logFile = new File(getFilesDir(), "logs/echoLink.log"); // 日志文件的路径
// 创建日志文件的路径
File logFile = new File(getFilesDir(), "logs/echoLink.log");
if (!logFile.exists()) {
return "日志文件不存在";
}
// 用于存储日志文件内容的StringBuilder
StringBuilder logContent = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(logFile))) {
String line;
// 逐行读取日志文件并将其添加到logContent中
while ((line = reader.readLine()) != null) {
logContent.append(line).append("\n");
}
}
// 将logContent转换为字符串并返回
return logContent.toString();
} catch (IOException e) {
e.printStackTrace();