Update MainActivity.java

dev
wangsiyuan 2024-04-22 10:59:23 +08:00
parent db097f7dd9
commit 81af975717
1 changed files with 31 additions and 0 deletions

View File

@ -5,6 +5,8 @@ import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@ -19,8 +21,10 @@ import com.nbee.echolink.utils.ShellUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import timber.log.Timber;
@ -34,6 +38,7 @@ public class MainActivity extends AppCompatActivity {
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("初始化日志...");
@ -63,6 +68,13 @@ public class MainActivity extends AppCompatActivity {
//启动通知监听服务
Intent serviceIntent1 = new Intent(this, NotificationListener.class);
startService(serviceIntent1);
clearAllLogs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearLogs(); // 调用清空日志的方法
}
});
}
@Override
@ -114,6 +126,25 @@ public class MainActivity extends AppCompatActivity {
logTextView.setText(newLog);
}
private void clearLogs() {
File logFile = new File(getFilesDir(), "logs/echoLink.log");
if (logFile.exists()) {
try {
// 使用空字符串重写文件来清空内容
PrintWriter writer = new PrintWriter(logFile);
writer.print("");
writer.close();
updateLog("日志已清空。"); // 更新 TextView 显示日志已被清空
} catch (FileNotFoundException e) {
e.printStackTrace();
updateLog("清空日志文件时出错。");
}
} else {
updateLog("日志文件不存在,无需清空。");
}
}
private String readLogFile() {
try {
File logFile = new File(getFilesDir(), "logs/echoLink.log"); // 日志文件的路径