[英]Does a Service in Android stop when the calling Activity/Fragment changes orientation?
[英]Continue tracking location in a service when user changes app or activity/fragment
我正在制作健身应用,并且需要在后台跟踪位置(使用Google Play服务)-我运行的是未绑定服务。 从片段开始运行时,该服务运行良好,但是当用户更改片段或更改/最小化应用程序(例如开始使用gmail应用程序等)时,该服务将停止。 我如何保持跟踪位置,以便在最小化应用程序然后再次打开它时仍将对其进行跟踪(以便它可以连续写入SQLite DB-行进距离,平均速度等)
片段中的代码段:
public void onResume(){
//setting up Broadcast Reciever
if(broadcastReceiver == null){
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Location currentLocation;
//retrieving extras with given tag from service
currentLocation = (Location) intent.getExtras().get("currentLocation");
//displaying speed and altitude if user has selected to view it from the spinner
if(appPreferences.getKeyStatsSpinner1().equalsIgnoreCase("CURRENT SPEED")){
spinner1TextView.setText(String.valueOf(currentLocation.getSpeed()));
}
if(appPreferences.getKeyStatsSpinner2().equalsIgnoreCase("CURRENT SPEED")){
spinner2TextView.setText(String.valueOf(currentLocation.getSpeed()));
}
if(appPreferences.getKeyStatsSpinner1().equalsIgnoreCase("CURRENT ALTITUDE")){
spinner1TextView.setText(String.valueOf(currentLocation.getAltitude()));
}
if(appPreferences.getKeyStatsSpinner2().equalsIgnoreCase("CURRENT ALTITUDE")){
spinner2TextView.setText(String.valueOf(currentLocation.getAltitude()));
}
}
};
}
//start recieving updates from service with tag given
//getActivity().registerReceiver(broadcastReceiver, new IntentFilter("LocationTrackingService_update"));
context.registerReceiver(broadcastReceiver, new IntentFilter("LocationTrackingService_update"));
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onPause: service cancelled");
//close connecting with broadcast receiver
if(broadcastReceiver != null){
//getActivity().unregisterReceiver(broadcastReceiver);
context.unregisterReceiver(broadcastReceiver);
}
trackLocation(false);
}
private void trackLocation(Boolean trackLocation){
if(trackLocation == true) {
Intent locationTrackingService = new Intent(context, LocationTrackingService.class);
//getActivity().startService(locationTrackingService);
context.startService(locationTrackingService);
}
else{
Intent locationTrackingService = new Intent(context, LocationTrackingService.class);
//getActivity().stopService(locationTrackingService);
context.stopService(locationTrackingService);
}
}
服务中的代码段:
//automatically called when we .connect() to googleApiClient
@Override
public void onConnected(@Nullable Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
............................
return;
}
//sets location to last known location
currentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
//broadcasting intent with last known location
Intent publishLocationUpdate = new Intent("LocationTrackingService_update");
publishLocationUpdate.putExtra("currentLocation", currentLocation);
sendBroadcast(publishLocationUpdate);
//request updates
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
我已经读过也许我应该使用“ startForegroundService”代替。 我还想,如果我从onPause中删除服务销毁,该服务将无限期运行-但是当我从onPause中删除代码时,该服务将继续正常运行。
没错,您应该使用ForegroundService。 以下示例运行完美。 我们正在项目中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.