android - 当应用程序进入后台时,计时器在 10 分钟后停止
问题描述
我需要在应用程序打开时或在后台每 5 秒发送一次位置,它工作 10 分钟。
计时器被用于每 5 秒发送一次位置。我搜索了很多所以计时器不是一个好方法,我不想更改我的服务代码,那么在后台连续工作的最佳方法是什么。服务在后台工作,但计时器停止。
@Override
public void onCreate() {
System.out.println("Location Service onCreate--------");
gps = new GPSTracker(LocationService.this);
socket();
T.scheduleAtFixedRate(mTt1, 5000, 5000);
}
TimerTask mTt1 = new TimerTask() {
public void run() {
mHandler12.postDelayed(mn_Runnable12, 5000);
}
};
public Runnable mn_Runnable12 = new Runnable() {
public void run() {
gps = new GPSTracker(LocationService.this);
if (gps.canGetLocation()) {
latString = Double.toString(gps.getLatitude()); // Live
logString = Double.toString(gps.getLongitude());
connection = MyApplication.getInstance().getConnection();
if (connection != null && connection.isConnected()) {
sendLocation();
} else {
//System.out.println("mn_Runnable12-------else");
}
}
}
};
解决方案
推荐阅读
- c# - .net core COM 与 Excel 范围
- r - 使用字符串向量在 R 中循环自定义 ifelse 函数
- firebase - Google Firebase 的计划政策是“每个帐户”还是“每个项目”?
- r - 如何将此列表转换为每行都是单个实体的数据框
- c# - 在指定文化和源时区的同时将“Kind-less”日期时间转换为世界时间
- javascript - 多次调用函数但丢失过去的数据
- office365 - SysWow64 中的 ActiveX 控件不适用于 Excel
- flutter - 如何将唯一 ID 或密钥分配给 SwitchListTile 并在颤动移动应用程序的 onChanged 中检索/获取其值
- javascript - 如何在 JavaScript 中为特定列表加下划线
- java - 使用 Tomcat 获取 AccessDeniedException?