5

我正在使用 jobScheduler 来获取后台位置更新。但是每次调度作业时,FusedLocationProviderClient 为空。这是为什么?我检查了 if(FusedLocationProviderClient == null) 条件,每次调度作业时,它下面的代码都会运行(这意味着 fusedLocationProviderClient 在初始化后为空。)请看下面的代码。此外,locationAvailability 通常是错误的,因此不会调用 onLocationResult 来给出空位置值。如何优化 FusedLocationProviderClient。还有一件事,fusedLocationProviderClient 是否总是为空,而 locationAvailability 是否与 false 相关?

@Override
public boolean onStartJob(JobParameters jobParameters) {
    Log.e("onStartJob", "onStartJob");//for debug
    jobP = jobParameters;

    if (!checkAndRequestPermissions()) {
        Toast.makeText(this, "Please provide location permission for paramount app.", Toast.LENGTH_LONG).show();
        provider = null;
    } else {
        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) {
        }
        if (mLocationRequest == null) {
            Log.e("onStartJob", "LocationRequest initialized"); //for debug
            mLocationRequest = LocationRequest.create();
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            mLocationRequest.setInterval(100 * 1000);
            mLocationRequest.setFastestInterval(60 * 1000);
        }

        if (client == null) {
            Log.e("onStartJob", "client initialized"); //for debug
            client = LocationServices.getFusedLocationProviderClient(this);
            client.requestLocationUpdates(mLocationRequest, new LocationCallback() {
                @Override
                public void onLocationResult(LocationResult locationResult) {
                    Log.e("onLocationResult ", "onLocationResult");
                    onLocationChanged(locationResult.getLastLocation());
                }
                @Override
                public void onLocationAvailability(LocationAvailability locationAvailability) {
                    Log.e("onLocationAvailability", locationAvailability + "");;
                }
            },
            Looper.myLooper());
        }

        try {
            provider = Settings.Secure.getInt(getContentResolver(), Settings.Secure.LOCATION_MODE) + "";
            gpsProvider = provider;
        } catch (Settings.SettingNotFoundException e) {
            Log.e("provider", "gps provider error");
        }
    }
    return true;
}

@Override
public boolean onStopJob(JobParameters jobParameters) {
    Log.e("onStopJob", "onStopJob");//for debug
    if (ul != null) {
        ul.cancel(true);
    }
    return true;
}

public void onLocationChanged(Location location) {
    latitude = location.getLatitude() + "";
    longitude = location.getLongitude() + "";
    Log.e("latitude" , latitude);
}

上述代码中的日志值如下所示:

03-15 17:09:25.889 10687-10687/com.myProject.com.jobschedulers E/onStartJob: onStartJob
03-15 17:09:25.900 10687-10687/com.myProject.com.jobschedulers E/onstartJob: client initialized
03-15 17:09:25.957 10687-10687/com.myProject.com.jobschedulers E/onLocationResult: onLocationResult
03-15 17:09:25.960 10687-10687/com.myProject.com.jobschedulers E/onLocationAvailability: LocationAvailability[isLocationAvailable: true]
03-15 17:23:26.975 10687-10687/com.myProject.com.jobschedulers E/onStartJob: onStartJob
03-15 17:23:26.993 10687-10687/com.myProject.com.jobschedulers E/onstartJob: client initialized
03-15 17:23:27.017 10687-10687/com.myProject.com.jobschedulers E/onLocationAvailability: LocationAvailability[isLocationAvailable: false]
03-15 17:41:32.672 10687-10687/com.myProject.com.jobschedulers E/onStartJob: onStartJob
03-15 17:41:32.690 10687-10687/com.myProject.com.jobschedulers E/onstartJob: client initialized
03-15 17:41:32.741 10687-10687/com.myProject.com.jobschedulers E/onLocationAvailability: LocationAvailability[isLocationAvailable: false]
03-15 17:53:17.335 10687-10687/com.myProject.com.jobschedulers E/onStartJob: onStartJob
03-15 17:53:17.351 10687-10687/com.myProject.com.jobschedulers E/onstartJob: client initialized
03-15 17:53:17.383 10687-10687/com.myProject.com.jobschedulers E/onLocationAvailability: LocationAvailability[isLocationAvailable: false]
4

2 回答 2

0

我经历了同样的问题。

我犯的第一个错误是不保证removeLocationUpdates会在与requestLocationUpdates. 实际上,它不必是同一个线程,但是在 a 之后requestLocationUpdates,您必须调用removeLocationUpdates以使下一个requestLocationUpdates有效。为了确保这一点,在同一个线程上工作要容易得多。

例如:

  private fun FusedLocationProviderClient.requestLocation(
      request: LocationRequest
  ): Single<LocationResult> {
    return Single.create<LocationResult> { emitter ->
      requestLocationUpdates(request, object : LocationCallback() {
        override fun onLocationResult(result: LocationResult?) {
          removeLocationUpdates(object : LocationCallback() {})
              .addOnCompleteListener {
                if (emitter.isDisposed) {
                  info("onLocationResult called after disposing.")
                  return@addOnCompleteListener
                }

                if (result != null && result.locations.isNotEmpty()) {
                  onSuccess(result)
                } else {
                  onError(RuntimeException("Invalid location result"))
                }
              }
        }

        private fun onError(error: Exception) {
          if (!emitter.isDisposed) {
            emitter.onError(error)
          }
        }

        private fun onSuccess(item: LocationResult) {
          if (!emitter.isDisposed) {
            emitter.onSuccess(item)
          }
        }

      }, Looper.getMainLooper())
    }
  }

正如代码所暗示的那样,我已经将 Single 吸引到emitterinaddOnCompleteListenerremoveLocationUpdates确保. 当然,如果没有 RxJava,它会更容易实现。removeLocationUpdatesrequestLocationUpdates

我犯的第二个错误intervalLocationRequest. 根据文档:

此方法以毫秒为单位设置您的应用希望接收位置更新的速率。请注意,位置更新可能比此速率更快或更慢以优化电池使用,或者可能根本没有更新(例如,如果设备没有连接)。

interval触发安卓系统的位置更新事件。如果requestLocationUpdates调用时没有位置更新,则onLocationResult回调将永远不会被调用,直到发生新的位置更新。

我犯的第三个错误是priority在 LocationRequest 中设置错误。在 API 10 及更低版本中,它不是,PRIORITY_BALANCED_POWER_ACCURACY但它已通过使用PRIORITY_HIGH_ACCURACY. 在这种情况下,我只在模拟器上进行了测试,因此实际设备可能会有不同的结果。我猜PRIORITY_BALANCED_POWER_ACCURACY它似乎无法正常工作,因为模拟器不提供蓝牙硬件。

所以我的LocationRequest样子:

LocationRequest.apply {
  priority = PRIORITY_HIGH_ACCURACY
  interval = 10000L
}

希望我犯的三个错误和解决方法对你有帮助!

于 2019-09-10T16:45:55.043 回答
0

JobScheduler 仅来自 21 个 API,请考虑使用 launch JobIntentServicefrom BroadcastReceiverfromPendingIntent和 start it all fromFusedLocationProviderClient.requestLocationUpdates方法。它看起来像这样:

class MainActivity : Activity() {
    ...
    lateinit var  mFusedLocationClient:FusedLocationProviderClient
    lateinit var mLocationRequestBackground: LocationRequest
    fun init {
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(activity = this)
        mLocationRequestBackground = LocationRequest()
        mLocationRequestBackground.interval = UPDATE_INTERVAL_IN_MILLISECONDS_BACKGROUND // i.e. 15 minutes - you hope to get updates at this interval, in background it will be not more than few times per hour on Android Oreo
        mLocationRequestBackground.fastestInterval = FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS_BACKGROUND //i.e. 1 minute - updates will not come faster than this
        mLocationRequestBackground.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY //do not overwhelm user's device with requests, but use requests from other apps
        mLocationRequestBackground.smallestDisplacement = 25f //distance in meters to trigger update
    }
    ...
    fun onStop() {
        //start updating in background
        mFusedLocationClient.requestLocationUpdates(mLocationRequestBackground,
                    LocationBroadcastReceiver.getPendingIntent(activity = this))
    }
    ...
}

class LocationBroadcastReceiver : BroadcastReceiver() {
    companion object {
        const val EXTRA_LATITUDE = "EXTRA_LATITUDE"
        const val EXTRA_LONGITUDE = "EXTRA_LONGITUDE"
        const val WAKEUP = "com.appid.intent.action.WAKEUP"
        fun getPendingIntent(context: Context, userId: String? = null): PendingIntent {
            val alarmIntent = Intent(context, LocationBroadcastReceiver::class.java)
            alarmIntent.action = WAKEUP
            return PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT)
        }
    }

    override fun onReceive(context: Context?, intent: Intent?) {
        context ?: return
        if (intent?.action != WAKEUP) return

        val location: LocationResult? = LocationResult.extractResult(intent)
        val loc: Location? = location?.lastLocation
        loc?.apply {
                val i = Intent(context, SendLocationService::class.java)
                i.putExtra(EXTRA_LATITUDE, latitude)
                i.putExtra(EXTRA_LONGITUDE, longitude)
                SendLocationService.enqueueWork(context, i)
        }
    }
}

class SendLocationService : JobIntentService() {
    companion object {
        private const val JOB_ID = 1000
        const val TAG = "SendLocationService"
        fun enqueueWork(context: Context, work: Intent) {
            enqueueWork(context, SendLocationService::class.java, JOB_ID, work)
        }
    }

    private var sending: Boolean = false
    override fun onHandleWork(intent: Intent) = runBlocking {
        val latitude = intent.getDoubleExtra(LocationBroadcastReceiver.EXTRA_LATITUDE, 0.0)
        val longitude = intent.getDoubleExtra(LocationBroadcastReceiver.EXTRA_LONGITUDE, 0.0)
        suspendCoroutine<Unit> { continuation ->
            YourApi.postMyLocation(lat = latitude, lng = longitude)
                    .callback { continuation.resume(Unit) }
        }
    }

    override fun onStopCurrentWork(): Boolean {
        return !sending
    }
}

不要忘记将它们添加到清单

    <service android:name=".services.SendLocationService"
        android:exported="false"
        android:permission="android.permission.BIND_JOB_SERVICE"/>
    <receiver android:name=".services.LocationBroadcastReceiver"
        android:exported="false"/>
于 2018-07-11T12:02:05.513 回答