Compare commits
2 Commits
e1241a01c3
...
8bfdaf761c
| Author | SHA1 | Date | |
|---|---|---|---|
| 8bfdaf761c | |||
| 33fa742d2e |
@@ -9,6 +9,7 @@ import com.google.gson.JsonSyntaxException;
|
|||||||
import com.nbee.echolink.R;
|
import com.nbee.echolink.R;
|
||||||
import com.nbee.echolink.model.DeviceInfo;
|
import com.nbee.echolink.model.DeviceInfo;
|
||||||
import com.nbee.echolink.response.ApiResponse;
|
import com.nbee.echolink.response.ApiResponse;
|
||||||
|
import com.nbee.echolink.service.MonitorService;
|
||||||
import com.nbee.echolink.utils.DeviceInfoUtils;
|
import com.nbee.echolink.utils.DeviceInfoUtils;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -29,6 +30,8 @@ public class HeartbeatAlarmReceiver extends BroadcastReceiver {
|
|||||||
context = context1;
|
context = context1;
|
||||||
// 例如,你可以启动一个服务或者直接在这里执行网络请求
|
// 例如,你可以启动一个服务或者直接在这里执行网络请求
|
||||||
sendHeartbeatSignal();
|
sendHeartbeatSignal();
|
||||||
|
// 重新设置下一个闹钟
|
||||||
|
MonitorService.scheduleHeartbeat(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendHeartbeatSignal() {
|
private void sendHeartbeatSignal() {
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ public class MonitorService extends Service {
|
|||||||
Timber.d("监控服务 onCreate");
|
Timber.d("监控服务 onCreate");
|
||||||
startForegroundService();
|
startForegroundService();
|
||||||
networkUtil = new NetworkUtil(this);
|
networkUtil = new NetworkUtil(this);
|
||||||
scheduleHeartbeat(); // 启动心跳定时任务
|
scheduleHeartbeat(this); // 启动心跳定时任务
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
@@ -140,10 +140,10 @@ public class MonitorService extends Service {
|
|||||||
return null; // 不提供绑定服务的接口
|
return null; // 不提供绑定服务的接口
|
||||||
}
|
}
|
||||||
|
|
||||||
private void scheduleHeartbeat() {
|
public static void scheduleHeartbeat(Context context) {
|
||||||
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||||
Intent intent = new Intent(this, HeartbeatAlarmReceiver.class); // HeartbeatAlarmReceiver是一个BroadcastReceiver
|
Intent intent = new Intent(context, HeartbeatAlarmReceiver.class);
|
||||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
|
||||||
|
|
||||||
long intervalMillis = HEARTBEAT_INTERVAL_MINUTES * 60 * 1000; // 10分钟的毫秒数
|
long intervalMillis = HEARTBEAT_INTERVAL_MINUTES * 60 * 1000; // 10分钟的毫秒数
|
||||||
|
|
||||||
@@ -155,4 +155,5 @@ public class MonitorService extends Service {
|
|||||||
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + intervalMillis, pendingIntent);
|
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + intervalMillis, pendingIntent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user