[英]Android O kills my Service as soon as it goes to the background
我已经阅读了许多其他类似的问题,并查看了有关背景执行限制( https://developer.android.com/about/versions/oreo/background#services )的Android文档,但仍然找不到解决方案。 我也尝试了大多数手机设置。
问题:我有一个应用程序,用于在后台收集位置数据。 我正在使用服务来实现这一目标。 目前,它每5秒钟收集一次位置数据。 它在我的Nexus 5(API 23)上运行良好; 但是,当应用程序位于前台时,它仅适用于我的Nexus 5X(API 27)。 一旦它进入后台,它就会停止。 因此,这与后台长时间运行的任务无关,一旦我离开该应用程序,该服务就会立即停止。
这是我的服务类代码:
import android.app.Service;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import app.khash.climbcollector.DataBase.DataContract.DataEntry;
public class GPSService extends Service implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener {
private LocationRequest mLocationRequest;
private GoogleApiClient mGoogleApiClient;
private static final String TAG = GPSService.class.getSimpleName();
@Override
public void onCreate() {
super.onCreate();
buildGoogleApiClient();
}//onCreate
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!mGoogleApiClient.isConnected())
mGoogleApiClient.connect();
return START_STICKY;
}//onStartCommand
@Override
public void onConnected(Bundle bundle) {
startLocationUpdate();
}//onConnected
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "onConnectionSuspended " + i);
}//onConnectionSuspended
@Override
public void onLocationChanged(Location location) {
//insert location to the database passing in the location object
insertDataToDb(location);
}//onLocationChanged
//method for adding the location data to db
private void insertDataToDb(Location location) {
final DateFormat dateFormat = new SimpleDateFormat("MM.dd.yyyy 'at' HH:mm:ss z");
//Current date and time using the format declared at the beginning
final String currentDateTime = dateFormat.format(Calendar.getInstance().getTime());
double lat = location.getLatitude();
double lng = location.getLongitude();
double alt = location.getAltitude();
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String key = getString(R.string.route_name_intent_extra);
String routeName = sharedPref.getString(key, "default");
// Create a new map of values,
ContentValues values = new ContentValues();
values.put(DataEntry.COLUMN_DATA_LATITUDE, lat);
values.put(DataEntry.COLUMN_DATA_LONGITUDE, lng);
values.put(DataEntry.COLUMN_DATA_ALTITUDE, alt);
values.put(DataEntry.COLUMN_DATA_DATE, currentDateTime);
values.put(DataEntry.COLUMN_DATA_ROUTE_NAME, routeName);
// Insert a new location into the provider, returning the content URI for the new location.
Uri newUri = getContentResolver().insert(DataEntry.CONTENT_URI, values);
// Show a toast message depending on whether or not the insertion was successful
if (newUri == null) {
// If the new content URI is null, then there was an error with insertion.
Log.v(TAG, "error saving data");
} else {
//since the insert method return the Uri of the row created, we can extract the ID of
//the new row using the parseID method with our newUri as an input. This method gets the
//last segment of the Uri, which is our new ID in this case and we store it in an object
// And add it to the confirmation method.
String id = String.valueOf(ContentUris.parseId(newUri));
// Otherwise, the insertion was successful and we can log
Log.v(TAG, "Successfully added: " + id);
}
}//insertDataToDb
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(TAG, "onConnectionFailed ");
}//onConnectionFailed
private void initLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(2000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}//initLocationRequest
private void startLocationUpdate() {
initLocationRequest();
//check for location permission
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
return;
}//check permission
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}//startLocationUpdate
private void stopLocationUpdate() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addOnConnectionFailedListener(this)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.build();
}//buildGoogleApiClient
@Override
public void onDestroy() {
super.onDestroy();
mGoogleApiClient.disconnect();
}//onDestroy
}//GPSService
我使用以下方法从我的主要活动中调用该服务:
Intent serviceStartIntent = new Intent(this, GPSService.class);
startService(serviceStartIntent);
并使用以下代码停止它:
Intent serviceStopIntent = new Intent(this, GPSService.class);
stopService(serviceStopIntent);
这是我尝试使用JobScheduler的代码。
public class GPSJobService extends JobService implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener {
private String TAG = GPSJobService.class.getSimpleName();
private LocationRequest mLocationRequest;
private GoogleApiClient mGoogleApiClient;
private JobParameters mParam;
@Override
public void onCreate() {
Log.v(TAG, "onCreate called");
super.onCreate();
buildGoogleApiClient();
}//onCreate
@Override
public void onDestroy() {
Log.v(TAG, "onDestroy called");
super.onDestroy();
mGoogleApiClient.disconnect();
}//onDestroy
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v(TAG, "onStartCommand called");
if (!mGoogleApiClient.isConnected())
mGoogleApiClient.connect();
return START_STICKY;
}//onStartCommand
@Override
public boolean onStartJob(JobParameters params) {
Log.v(TAG, "onStartJob called");
if (!mGoogleApiClient.isConnected())
mGoogleApiClient.connect();
mParam = params;
return true;
}//onStartJob
@Override
public void onLocationChanged(Location location) {
//insert location to the database passing in the location object
insertDataToDb(location);
}//onLocationChanged
private void initLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(2000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}//initLocationRequest
private void startLocationUpdate() {
initLocationRequest();
//check for location permission
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
return;
}//check permission
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}//startLocationUpdate
//method for adding the location data to db
private void insertDataToDb(Location location) {
final DateFormat dateFormat = new SimpleDateFormat("MM.dd.yyyy 'at' HH:mm:ss z");
//Current date and time using the format declared at the beginning
final String currentDateTime = dateFormat.format(Calendar.getInstance().getTime());
double lat = location.getLatitude();
double lng = location.getLongitude();
double alt = location.getAltitude();
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
String key = getString(R.string.route_name_intent_extra);
String routeName = sharedPref.getString(key, "default");
// Create a new map of values,
ContentValues values = new ContentValues();
values.put(DataEntry.COLUMN_DATA_LATITUDE, lat);
values.put(DataEntry.COLUMN_DATA_LONGITUDE, lng);
values.put(DataEntry.COLUMN_DATA_ALTITUDE, alt);
values.put(DataEntry.COLUMN_DATA_DATE, currentDateTime);
values.put(DataEntry.COLUMN_DATA_ROUTE_NAME, routeName);
// Insert a new location into the provider, returning the content URI for the new location.
Uri newUri = getContentResolver().insert(DataEntry.CONTENT_URI, values);
// Show a toast message depending on whether or not the insertion was successful
if (newUri == null) {
// If the new content URI is null, then there was an error with insertion.
Log.v(TAG, "error saving data");
} else {
//since the insert method return the Uri of the row created, we can extract the ID of
//the new row using the parseID method with our newUri as an input. This method gets the
//last segment of the Uri, which is our new ID in this case and we store it in an object
// And add it to the confirmation method.
String id = String.valueOf(ContentUris.parseId(newUri));
// Otherwise, the insertion was successful and we can log
Log.v(TAG, "Successfully added: " + id);
//finish the job
jobFinished(mParam, true);
}
}//insertDataToDb
@Override
public boolean onStopJob(JobParameters params) {
Log.v(TAG, "onStopJob called");
return false;
}//onStopJob
@Override
public void onConnected(@Nullable Bundle bundle) {
startLocationUpdate();
}//onConnected
@Override
public void onConnectionSuspended(int i) {
Log.v(TAG, "onConnectionSuspended called");
}//onConnectionSuspended
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.v(TAG, "onConnectionFailed called");
}//onConnectionFailed
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addOnConnectionFailedListener(this)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.build();
}//buildGoogleApiClient
}//GPSJobService
我在这样的主要活动中称呼它为:
case R.id.bttn_job_start:
ComponentName serviceComponent = new ComponentName(this, GPSJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(mJobId, serviceComponent);
builder.setMinimumLatency(5000);
builder.setBackoffCriteria(5000, JobInfo.BACKOFF_POLICY_LINEAR);
//repeat every 5 seconds
// builder.setPeriodic(5000);
JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(builder.build());
Toast.makeText(this, "Started", Toast.LENGTH_SHORT).show();
break;
我尝试设置延迟和退避条件,或者设置为定期(现在注释掉)。 这两种方法都可以在应用程序处于前台时起作用。 当应用程序进入后台时,两者均无效。
正如我之前提到的,它在Nexus 5上可以完美运行,但在Nexus 5X上却不能。
关于如何解决此问题的任何建议?
谢谢,
它的行为符合预期。 从Android o开始,当应用程序处于前台时,它可以自由创建和运行前台和后台服务。 当应用进入后台时,它会有一个几分钟的窗口(根据我的观察,大约是1-2分钟),仍然可以在其中创建和使用服务。 系统停止应用程序的后台服务,就像该应用程序已调用服务的Service.stopSelf()
方法一样。
如果需要立即且可靠地完成任务,那么最好的替代方法是使用ForegroundService
。 您可以遵循描述这种方法的SO 。
如果需要在满足某些约束的稍后时间执行作业,请考虑使用JobScheduler或WorkManager 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.