如何解决屏幕锁定时,前台服务进入睡眠状态
我希望将用户的位置同步到服务器。我已经为此编写了前台服务。但是,当屏幕锁定时,该服务将停止发送任何更新。我该如何解决?
public final class LocationUpdateService extends Service
{
private static final String TAG = "LocationUpdateService";
private FusedLocationProviderClient fusedLocationProviderClient;
private LocationRequest locationRequest;
private LocationCallback locationCallback;
private Util util = new Util();
private static final long LOCATION_INTERVAL = 15000;
private static final long LOCATION_MIN_DISPLACEMENT = 20;
private Handler serviceHandler;
@Nullable
@Override
public IBinder onBind(Intent intent)
{
return null;
}
@Override
public void onCreate()
{
/*HandlerThread handlerThread = new HandlerThread(TAG);
handlerThread.start();
serviceHandler = new Handler(handlerThread.getLooper());*/
fusedLocationProviderClient = new FusedLocationProviderClient(getApplicationContext());
locationRequest = LocationRequest.create();
locationRequest.setInterval(LOCATION_INTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
// locationRequest.setSmallestDisplacement(LOCATION_MIN_DISPLACEMENT);
locationCallback = new LocationCallback()
{
@Override
public void onLocationResult(LocationResult locationResult)
{
if (locationResult == null)
{
return;
}
Location location = locationResult.getLastLocation();
if (location != null)
{
Log.d(TAG,"LocationCallback: location received");
sendLocation(util.getToken(getApplicationContext()),new RequestLocationUpdate(String.valueOf(location.getLatitude()),String.valueOf(location.getLongitude())));
}
else
{
Log.d(TAG,"LocationCallback: null location received");
}
}
};
startForeground(Constants.NOTIFICATION_ID_LOCATION,getLocationNotification());
requestLocationUpdates();
}
@Override
public int onStartCommand(Intent intent,int flags,int startId)
{
return START_STICKY;
}
@Override
public void onDestroy()
{
super.onDestroy();
removeLocationUpdates();
serviceHandler.removeCallbacksAndMessages(null);
stopSelf();
}
@Override
public void onTaskRemoved(Intent rootIntent)
{
Intent restartServiceIntent = new Intent(getApplicationContext(),this.getClass());
restartServiceIntent.setPackage(getPackageName());
PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(),1,restartServiceIntent,PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(
AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime() + 1000,restartServicePendingIntent);
super.onTaskRemoved(rootIntent);
}
private void requestLocationUpdates()
{
fusedLocationProviderClient.requestLocationUpdates(locationRequest,locationCallback,Looper.getMainLooper());
}
private void removeLocationUpdates()
{
fusedLocationProviderClient.removeLocationUpdates(locationCallback);
}
private Notification getLocationNotification()
{
return new NotificationCompat.Builder(this,Constants.NOTIFICATION_CHANNEL_IMPORTANT_ID)
.setContentTitle("Location Service")
.setContentText("Your location is being synced to the server")
.setSmallIcon(R.drawable.vector_logo)
.build();
}
private void sendLocation(String authorization,RequestLocationUpdate request)
{
APIClient.getClient().updateLocation(authorization,request).enqueue(new Callback<ResponseGenericUser>()
{
@Override
public void onResponse(Call<ResponseGenericUser> call,Response<ResponseGenericUser> response)
{
if (response.isSuccessful())
{
if (response.body().getStatus())
{
Log.d(TAG,"sendLocation: Location sent successfully");
} else
{
Log.d(TAG,"sendLocation: " + response.body().getMessage());
}
} else
{
Log.d(TAG,"sendLocation: " + response.message());
}
}
@Override
public void onFailure(Call<ResponseGenericUser> call,Throwable t)
{
Log.d(TAG,"sendLocation: " + t.getMessage());
}
});
}
}
解决方法
奇怪的是,解决方案是使用locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
而不是locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
。
进行此更改后,它也开始在锁定屏幕的情况下工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。