【问题标题】:Fused Location Provider GoogleApiClient sometimes not updating location in background serviceFused Location Provider GoogleApiClient 有时不会在后台服务中更新位置
【发布时间】:2016-07-22 11:03:04
【问题描述】:

以下是我的位置服务类,如果设备更改位置,它会继续更新位置。

public class LocationService extends Service
        implements LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {
    private static final long INTERVAL = 1000 * 60;
    private static final long FASTEST_INTERVAL = 1000 * 5;
    Location mLastLocation;
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    String lat, lon;


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("", "********************** onStartCommand()");
        int permissionCheck = ContextCompat.checkSelfPermission(LocationService.this, Manifest.permission.ACCESS_FINE_LOCATION);

        if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
            //Execute location service call if user has explicitly granted ACCESS_FINE_LOCATION..
            buildGoogleApiClient();
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d("", "********************** onBind()");
        return null;
    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.d("", "********************** onConnected()");
//        Util.appendLog("Location Service onConnected()");
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            lat = String.valueOf(mLastLocation.getLatitude());
            lon = String.valueOf(mLastLocation.getLongitude());

            Util.WriteSharePrefrence(getApplicationContext(), "track_lat", "" + lat);
            Util.WriteSharePrefrence(getApplicationContext(), "track_lng", "" + lon);

            Util.appendLog("Location Service onConnected(): " + " Latitude: " + lat + " Longitude: " + lon);
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        Util.appendLog("Location Service onConnectionSuspended()");
    }

    synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

        mGoogleApiClient.connect();
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d("", "********************** onLocationChanged()");
//        Util.appendLog("Location updated Latitude: " + location.getLatitude() + " longitude: " + location.getLongitude());
//        Toast.makeText(LocationService.this, "Latitude: " + location.getLatitude() + " Longitude: " + location.getLongitude(), Toast.LENGTH_SHORT).show();
        if (location != null) {
            Util.WriteSharePrefrence(getApplicationContext(), "track_lat", "" + location.getLatitude());
            Util.WriteSharePrefrence(getApplicationContext(), "track_lng", "" + location.getLongitude());
        }
//        stopSelf();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Util.appendLog("Location Service onConnectionFailed()");
        buildGoogleApiClient();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        Util.appendLog("Location servcie destroyed()");
    }
}

在 Manifest 中,我已将这项服务声明如下:

<service
            android:name=".util.LocationService"
            android:enabled="true" />

每当用户在他/她的设备上启动应用程序时,我都会启动它

 if (!isMyServiceRunning(LocationService.class, context)) {
                    context.startService(new Intent(context, LocationService.class));
}

注意:我不会从任何地方停止服务。

问题:

  1. 所有大品牌设备的位置更新,如 google nexus 5、gionee、One+、三星....但在某些设备位置不更新时,如 Honor、xolo....
  2. 每当设备在户外或行驶中时,应调用onLocationChanged() 方法,并在WriteSharePrefrence() 中更改新更新的经纬度,但在某些设备中更新不频繁。
  3. 我的服务有什么问题吗?我的应用中没有任何耗电问题。
  4. 有什么东西阻止了我的服务?
  5. 如何继续在后台运行位置更新服务并频繁地从用户设备获取准确的位置更新?

【问题讨论】:

  • onDestroy 被调用了吗?如果设备内存不足,操作系统可能会终止您的服务。尝试添加标志-Service.START_STICKY
  • @X3Btel onDestrye 没有打电话。 Service.START_STICKY 发生了什么?
  • Start sticky 会在系统终止服务时重新启动服务。但是如果 onDestroy 没有被调用,它应该没有帮助。其他设备也开启了gps,精度高吗?
  • 是的,所有设备都有相同的代码和逻辑。

标签: android location fusedlocationproviderapi


【解决方案1】:

Google 的 Play Services API 已经很好地处理了这种情况。

看看这个:FusedLocationProviderApi requestLocationUpdates

您应该使用以下功能:

public abstract PendingResult<Status> requestLocationUpdates (GoogleApiClient client, LocationRequest request, PendingIntent callbackIntent)

链接中的关键亮点:

此方法适用于后台用例,尤其适用于接收位置更新,即使应用已被系统终止。对于前台用例,推荐使用LocationListener版本的方法...

只需使用requestLocationUpdates 的 PendingIntent 版本,您就应该开始可靠地接收后台位置更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-20
    • 1970-01-01
    相关资源
    最近更新 更多