我已经阅读了很多其他类似的问题,并检查了有关后台执行限制(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;
我已尝试设置延迟和退避条件,或设置为periodic(现在已注释掉)。当应用程序位于前台时,这两种方法都有效。当应用程序进入后台时,两者都无效。
正如我之前提到的,它在我的Nexus 5上运行得很好,但在Nexus 5X上运行不正确。
有关如何解决此问题的任何建议?
谢谢,
答案 0 :(得分:0)
表现如预期。从Android o开始,当应用程序位于前台时,它可以自由地创建和运行前台和后台服务。当一个应用程序进入后台时,它有一个几分钟的窗口(根据我的观察结果大约1 - 2分钟),它仍然允许创建和使用服务。系统会停止应用程序的后台服务,就像应用程序调用了服务一样。 Service.stopSelf()
方法。
如果任务需要立即可靠地完成,那么最好的替代方法是使用ForegroundService
。您可以按照SO来描述这种方法。
如果需要在稍后满足某些约束条件时执行作业,请考虑使用JobScheduler或WorkManager。