![](/img/trans.png)
[英]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.