Compare commits

...

4 Commits

Author SHA1 Message Date
wangsiyuan d500f8ee41 Update activity_main.xml 2024-04-22 10:59:27 +08:00
wangsiyuan 81af975717 Update MainActivity.java 2024-04-22 10:59:23 +08:00
wangsiyuan db097f7dd9 Update HeartbeatAlarmReceiver.java 2024-04-22 10:59:19 +08:00
wangsiyuan 2314852b1c Update BootCompletedReceiver.java 2024-04-22 10:59:13 +08:00
4 changed files with 98 additions and 21 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"); // 日志文件的路径

View File

@ -7,10 +7,17 @@ import android.content.Intent;
import com.nbee.echolink.service.MonitorService;
public class BootCompletedReceiver extends BroadcastReceiver {
/**
* 广
*
* @param context
* @param intent 广
*/
@Override
public void onReceive(Context context, Intent intent) {
// 检查接收到的广播是否为系统启动完成的广播
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
// 启动您的服务或活动
// 如果是系统启动完成的广播,则启动监控服务
Intent serviceIntent = new Intent(context, MonitorService.class);
context.startService(serviceIntent);
}

View File

@ -24,23 +24,40 @@ public class HeartbeatAlarmReceiver extends BroadcastReceiver {
private Context context;
private String accessToken;
private String SN;
/**
* 广
*
* @param context1 访
* @param intent 广
*/
@Override
public void onReceive(Context context1, Intent intent) {
// 在这里执行发送心跳的逻辑
// 初始化上下文对象为后续操作提供context
context = context1;
// 例如,你可以启动一个服务或者直接在这里执行网络请求
// 执行发送心跳信号的逻辑
sendHeartbeatSignal();
// 重新设置下一个闹钟
// 在发送心跳后,重新设置下一次心跳发送的时间
MonitorService.scheduleHeartbeat(context);
}
/**
*
*
*
*/
private void sendHeartbeatSignal() {
String heartBeatURL = context.getResources().getString(R.string.heart_beat_url); // 获取URL
// 获取心跳请求的URL
String heartBeatURL = context.getResources().getString(R.string.heart_beat_url);
OkHttpClient client = new OkHttpClient();
Request request = buildRequest(heartBeatURL);
// 发送异步请求
client.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(okhttp3.Call call, IOException e) {
// 请求失败时的处理逻辑
Timber.e(e, "请求 " + heartBeatURL + "失败");
}
@ -48,55 +65,67 @@ public class HeartbeatAlarmReceiver extends BroadcastReceiver {
public void onResponse(okhttp3.Call call, okhttp3.Response response) {
try {
if (!response.isSuccessful()) {
// 在这里处理响应错误
// 处理响应错误的情况
Timber.d("Request to " + heartBeatURL + " returned error: " + response.code() + ", " + response.message());
} else {
// 处理成功响应
String responseBody = response.body().string(); // 获取响应体
// 处理成功响应的逻辑
String responseBody = response.body().string(); // 获取响应体内容
Gson gson = new Gson();
ApiResponse apiResponse = gson.fromJson(responseBody, ApiResponse.class);
if (apiResponse == null){
if (apiResponse == null) {
// 处理api响应为空的情况
Timber.d("apiResponse is null.");
}
if (!apiResponse.getCode().equals("0")) {
// 处理响应状态码错误的情况
Timber.d("请求返回状态码错误,返回内容: " + apiResponse);
}
}
} catch (IOException e) {
// 处理响应处理过程中的IO异常
Timber.e(e, "IOException during handling response");
} catch (JsonSyntaxException e) {
// 处理响应解析过程中的JSON格式异常
Timber.e(e, "JsonSyntaxException during parsing response");
} finally {
response.close(); // 确保响应体被关闭
// 确保响应体被关闭,避免资源泄露
response.close();
}
}
});
}
private <T>Request buildRequest(String apiUrl){
/**
*
*
* @param apiUrl API
* @param <T>
* @return Request
*/
private <T> Request buildRequest(String apiUrl) {
// 从资源文件中读取序列号和访问令牌
SN = context.getResources().getString(R.string.SN);
accessToken = context.getResources().getString(R.string.access_token);
// 创建设备信息对象并设置设备相关属性
DeviceInfo deviceInfo = new DeviceInfo();
deviceInfo.setDeviceBrand(DeviceInfoUtils.getDeviceBrand());
deviceInfo.setDeviceModel(DeviceInfoUtils.getDeviceModel());
deviceInfo.setAndroidVersion("Android " + DeviceInfoUtils.getDeviceAndroidVersion());
deviceInfo.setSn(SN);
// 使用Gson将设备信息对象转换为JSON字符串
Gson gson = new Gson();
// 将对象转换为JSON字符串
String json = gson.toJson(deviceInfo);
// Timber.d("Building request to " + apiUrl + " with data: " + json);
// 创建请求体
// 创建请求体使用JSON格式
MediaType JSON = MediaType.get("application/json; charset=utf-8");
RequestBody body = RequestBody.create(json, JSON);
// 构建请求,并设置请求头
// 构建请求对象并设置请求头包括accessToken和Content-Type
Request request = new Request.Builder()
.url(apiUrl)
.addHeader("accessToken", accessToken) // 使用加载的accessToken
.addHeader("accessToken", accessToken)
.addHeader("Content-Type", "application/json")
.post(body)
.build();
return request;
}
}

View File

@ -5,6 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- 用于显示日志标题的 TextView -->
<TextView
android:id="@+id/logTitleTextView"
@ -19,10 +20,11 @@
<!-- ScrollView 包含一个 TextView 用于显示日志内容 -->
<ScrollView
android:id="@+id/logScrollView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/logTitleTextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@+id/clearLogsButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
@ -32,5 +34,13 @@
android:layout_height="wrap_content" />
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
<!-- 新增加的按钮用于清空所有日志 -->
<Button
android:id="@+id/clearLogsButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="清空所有日志"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>