Update HeartbeatAlarmReceiver.java
parent
2314852b1c
commit
db097f7dd9
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue