【发布时间】:2021-02-22 08:49:55
【问题描述】:
我已经使用警报管理器每 15 分钟将位置上传到 firebase 数据库,但是如果用户仍然在他所在的地方,我需要停止下一次更新,(下一次更新),可以使用融合位置 API或 android 中的位置管理器。当应用程序被杀死或 Android 系统处于深度渗透模式(锁定)时,这应该可以工作。所以我们需要后台定位权限,也可以!但它不应该一直打开定位服务,因为它会耗尽电池。所以定位服务应该停止,直到警报管理器下一次启动。
这是我用的TimerService!
public class TimerService extends BroadcastReceiver {
public static final int REQUEST_CODE = 12346;
// Triggered by the Alarm periodically (starts the service to run task)
@Override
public void onReceive(Context context, Intent intent) {
Intent serviceIntent = new Intent(context, TrackingService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
ContextCompat.startForegroundService(context, serviceIntent);
} else {
context.startService(serviceIntent);
}
}
}
这是 TrackingService 中的方法
private void requestLocationUpdates() {
LocationRequest request = new LocationRequest();
request.setInterval(10000);
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
client = LocationServices.getFusedLocationProviderClient(this);
int permission = ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION);
if (permission == PackageManager.PERMISSION_GRANTED) {
locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Location location = locationResult.getLastLocation();
if (location != null) {
updateFirebase(location);
}
}
};
client.requestLocationUpdates(request,locationCallback,null);
}else{
stopForeground(true);
stopSelf();
}
}
所以我需要帮助实现一个类似这样的新方法,
tryLocationUpdate(Location location){
if(location==stillInSamePlace){
//Do not upload to the firebase database
}else{
uploadLocation();
}
}
谁能帮帮我?
【问题讨论】:
标签: android location alarmmanager repeatingalarm