创建 HeartbeatAlarmReceiver.java
parent
1e71f67c59
commit
67900cd09a
|
|
@ -0,0 +1,100 @@
|
||||||
|
package com.nbee.echolink.broadcast;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.JsonSyntaxException;
|
||||||
|
import com.nbee.echolink.R;
|
||||||
|
import com.nbee.echolink.model.DeviceInfo;
|
||||||
|
import com.nbee.echolink.response.ApiResponse;
|
||||||
|
import com.nbee.echolink.utils.DeviceInfoUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
public class HeartbeatAlarmReceiver extends BroadcastReceiver {
|
||||||
|
private Context context;
|
||||||
|
private String accessToken;
|
||||||
|
private String SN;
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context1, Intent intent) {
|
||||||
|
// 在这里执行发送心跳的逻辑
|
||||||
|
context = context1;
|
||||||
|
// 例如,你可以启动一个服务或者直接在这里执行网络请求
|
||||||
|
sendHeartbeatSignal();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendHeartbeatSignal() {
|
||||||
|
Timber.d("开始执行发送心跳");
|
||||||
|
String heartBeatURL = context.getResources().getString(R.string.heart_beat_url); // 获取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 + "失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
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(); // 获取响应体
|
||||||
|
Gson gson = new Gson();
|
||||||
|
ApiResponse apiResponse = gson.fromJson(responseBody, ApiResponse.class);
|
||||||
|
if (apiResponse == null){
|
||||||
|
Timber.d("apiResponse is null.");
|
||||||
|
}
|
||||||
|
if (!apiResponse.getCode().equals("0")) {
|
||||||
|
Timber.d("请求返回状态码错误,返回内容: " + apiResponse);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
Timber.e(e, "IOException during handling response");
|
||||||
|
} catch (JsonSyntaxException e) {
|
||||||
|
Timber.e(e, "JsonSyntaxException during parsing response");
|
||||||
|
} finally {
|
||||||
|
response.close(); // 确保响应体被关闭
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
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 gson = new Gson();
|
||||||
|
// 将对象转换为JSON字符串
|
||||||
|
String json = gson.toJson(deviceInfo);
|
||||||
|
// Timber.d("Building request to " + apiUrl + " with data: " + json);
|
||||||
|
// 创建请求体
|
||||||
|
MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
||||||
|
RequestBody body = RequestBody.create(json, JSON);
|
||||||
|
|
||||||
|
// 构建请求,并设置请求头
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(apiUrl)
|
||||||
|
.addHeader("accessToken", accessToken) // 使用加载的accessToken
|
||||||
|
.addHeader("Content-Type", "application/json")
|
||||||
|
.post(body)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue