在一些新设备中几天后停止在后台接收位置,而在其他设备中工作正常

问题描述 投票:0回答:1

我做了一个主要用作跟踪器的应用程序。问题是,我几乎在每天和后台测试的设备中都能正常工作。测试:

摩托罗拉XT1563,自2018-02-02开始工作

摩托罗拉XT1635-02,自2018-01-24

三星E7,自2018-01-19

即使我重新启动,丢失连接,打开/关闭位置,这也很有效。

但在三星SM-G930F和三星SM-G950F在后台工作正常,可以接受3-4天的可用性,但在此之后停止发送位置并且从不对设备一无所知,至少用户再次打开应用程序。

我知道新的background location limits所以我不得不对我的应用程序进行一些更改。我遵循this指南。

位置要求:

private void createLocationRequest() {
        mLocationRequest = new LocationRequest()
                .setInterval(Constants.UPDATE_INTERVAL)
                .setFastestInterval(Constants.UPDATE_FASTEST_INTERVAL)
        .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    }
private synchronized void buildGoogleApiClient(Context context) {
        mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();}

开始位置更新

public void startLocationUpdates() {

        if (isLocationPermissionGranted()){

            Intent mRequestLocationUpdatesBroadcaster = new Intent(mContext, JobIntentBroadcaster.class);
            mPendingIntent= PendingIntent.getBroadcast(mContext, 0, mRequestLocationUpdatesBroadcaster,
                    PendingIntent.FLAG_UPDATE_CURRENT);
LocationServices.getFusedLocationProviderClient(mContext).requestLocationUpdates(mLocationRequest, mPendingIntent);

BroadcastReceiver:

public class JobIntentBroadcaster extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        intent.setClass(context, LocationIntentService.class);
        LocationIntentService.enqueueWork(context, intent);
    }
}

JobIntentService(在新的android版本之前使用):

public class LocationIntentService extends JobIntentService {

    static final int JOB_ID = 1000;

    public static void enqueueWork(Context context, Intent work) {
        Log.d("enqueueWork","Se llama al jobintent service!");
        enqueueWork(context, LocationIntentService.class, JOB_ID, work);
    }

@Override
protected void onHandleWork(@NonNull Intent intent) {
    if (LocationResult.hasResult(intent)) {
        LocationResult locationResult = LocationResult.extractResult(intent);
        Location location = locationResult.getLastLocation();
        @SuppressLint("SimpleDateFormat") String mLastUpdateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        if (location != null) { 
            send to server
        }

如何使用新设备处理此问题?

是否有必要发出通知(前台服务)来解决这个问题?

这是新的Android版本或三星电池安全的结果吗?

UPDATE

我发现这个问题是由三星电池优化引起的。 This是我找到的密切解决方案,但不符合我的要求所以问题仍然存在......

java android location google-api-client google-location-services
1个回答
0
投票

我使用Android X中的新WorkerManager类有一些更好但不完美的结果,它根据一些未公开的因素利用AlarmManagerJobScheduler或其他内部。

另一个探索的选择是Firebase Job Dispatcher

© www.soinside.com 2019 - 2024. All rights reserved.