簡體   English   中英

幾天后在某些新設備中停止在后台接收位置信息,而在其他設備中則可以

[英]Stop receiving location in background after few days in some new devices meanwhile in others works OK

我制作了一個主要用作跟蹤器的應用。 問題是在幾天內和在后台測試的幾乎所有設備中,它都能正常工作。 測試:

摩托羅拉XT1563,於2018-02-02工作

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

三星E7,自2018年1月19日

即使我重新啟動,失去連接,打開/關閉位置,這也能正常工作...

但是在Samsung SM-G930F和Samsung SM-G950F中,可以在3-4天的可接受頻率下在后台正常工作,但是此后停止發送位置,並且從不知道設備,至少用戶再次打開了該應用程序。

我知道新的背景位置限制,所以我必須對我的應用程序進行一些更改。 為此 ,我遵循指南。

位置要求:

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版本還是三星電池安全性的結果?

更新

我發現此問題是由三星電池優化引起的。 是我找到的接近解決方案,但不符合我的要求,因此問題仍然存在...

使用Android X中的新WorkerManager類, WorkerManager更好但不是完美的結果, JobScheduler根據一些未公開的因素在內部利用AlarmManagerJobScheduler或其他類。

探索的另一種選擇是Firebase Job Dispatcher

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM