简体   繁体   English

使用AsyncTaskLoader更新位置数据UI?

[英]Update Location data UI using AsyncTaskLoader?

I am using the FusedLocationProvider API for an app. 我正在为应用程序使用FusedLocationProvider API。 The location of the device should be updating regularly since the user is expected to be moving around all the time, and the app should behave differently depending on the current device location. 设备的位置应定期更新,因为用户应始终在移动,并且应用程序的行为应根据当前设备位置而有所不同。

In order to keep the Main Thread clear from network requests I wanted to move the code for retrieving location data into a seperate loader. 为了保持主线程不受网络请求的影响,我想将用于检索位置数据的代码移动到单独的加载器中。 However, I have no clue where to put this part of the code: 但是,我不知道在哪里放这部分代码:

locationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
            .setInterval(UPDATE_INTERVAL)
            .setFastestInterval(FASTEST_INTERVAL);


        try{
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
        }catch (SecurityException a){Log.w(TAG,"No location permission.");}

If I put it in the LoaderClass and call this method in loadInBackground(), there will be the error java.lang.NullPointerException: Calling thread must be a prepared Looper thread. 如果我把它放在LoaderClass中并在loadInBackground()中调用此方法,则会出现错误java.lang.NullPointerException: Calling thread must be a prepared Looper thread.

It also feels very wrong to call the Loader again and again. 一次又一次地召唤装载机也觉得非常错误。 So my question is how can I implement requestLocationUpdates using Loaders so that the Loader updates the changed location which I can retrieve in onLoadFinished() and update the corresponding UI? 所以我的问题是如何使用Loaders实现r​​equestLocationUpdates,以便Loader更新我可以在onLoadFinished()中检索的更改位置并更新相应的UI? And what should I do with the method onLocationChanged() of the Location Listener which is needed to be overridden? 我应该怎么做位置监听器的onLocationChanged()方法需要被覆盖? Or is there perhaps another effective way to move the FusedLocationProvider into a background thread? 或者是否有另一种有效的方法将FusedLocationProvider移动到后台线程?

It would be really great if someone can help me here, many many thanks! 如果有人能在这里帮助我真的很棒,非常感谢!

Or is there perhaps another effective way to move the FusedLocationProvider into a background thread? 或者是否有另一种有效的方法将FusedLocationProvider移动到后台线程? It would be really great if someone can help me here, many many thanks! 如果有人能在这里帮助我真的很棒,非常感谢!

If you wanna receive location updates in background thread, you can use Looper version of the requestLocationUpdates method. 如果您想在后台线程中接收位置更新,可以使用requestLocationUpdates方法的Looper版本。

requestLocationUpdates(GoogleApiClient, LocationRequest, LocationListener, Looper) requestLocationUpdates(GoogleApiClient,LocationRequest,LocationListener,Looper)

In that case, onLocationChanged method will be triggered on the looper's thread. 在这种情况下,将在looper的线程上触发onLocationChanged方法。

private HandlerThread mLocThread;
private void initThread(){
    mLocThread = new HandlerThread("locationThread");
    mLocThread.start();
}

private void requestLocUpdates(){
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, 
    locationRequest, this, mLocThread.getLooper());
}

@Override
public void onLocationChanged(Location location) {
    // locationThread thread
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM