Android:实现LocationManager的最佳实践

时间:2016-04-28 09:31:35

标签: android android-service locationmanager locationlistener

我正在使应用程序发送纬度和经度的坐标,所以我正在扩展服务并实现了LocationManager。 这是我的代码的一部分:

public class LocationServiceTracking extends Service implements LocationListener{
....
 @Override
    public void onCreate() {
        super.onCreate();
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

            if (isGPSEnabled && provider.equals(LocationManager.GPS_PROVIDER)) {
                try {
                    mLocationManager.requestLocationUpdates(3000, 0, criteria, this, null);
                    Log.d("GPS Enabled", "GPS Enabled");
                } catch (SecurityException se) {
                    se.printStackTrace();
                    Crashlytics.logException(se);
                }
            }
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

}

我的问题是¿如果您现在在onCreate ()中执行此操作或将其更改为onStartCommand (),最佳做法是什么?

4 个答案:

答案 0 :(得分:1)

阅读关于Android的FusedLocationProvider api。

它将处理大多数并发症。如果未启用gps,则需要来自网络提供商等的协调。 在这里阅读:

https://developers.google.com/android/reference/com/google/android/gms/location/FusedLocationProviderApi

答案 1 :(得分:1)

使用Fused location api以简洁起见。

关于您的问题 - 最好在onCreate()中执行此操作,因为onStartCommand()将在您触发启动服务时多次调用,而onCreate仅在实际创建服务时调用。

Calling start service

Service lifecycle

答案 2 :(得分:0)

应该在onStartCommand()

  

原因:只要创建服务,就会调用onCreate()   但是,当您手动启动时,将调用onStartCommand()   通过致电obj.startService()来提供服务。

因此,如果您将该代码放在onCreate()中,您将在启动服务之前开始接收更新,这不是一个好习惯。只有当启动绑定时,才能启用Service中编写的代码。

例如

MyService serviceObj = new MyService(); // this will call onCreate().

serviceObj.startService(); // this will call `onStartCommand()`.

serviceObj.bindService(); // this will call onBind(). (Not your case).

希望这有帮助。

答案 3 :(得分:0)

在您的班级中创建一个名为mGoogleApiClient的字段。

然后在onCreate中执行此操作:

// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
    mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

当类加载和停止时,处理该字段的生命周期:

public void onStart() {
    mGoogleApiClient.connect();
    super.onStart();
}

public void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

现在实现以下接口:

GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener

并添加他们的方法:

@Override
public void onConnected(Bundle connectionHint) {
    Log.d(TAG, "Google Maps API::onConnected");
    //For Marshmallow 6.0, make sure to check Permissions
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}

@Override
public void onConnectionSuspended(int i) {
    Log.d(TAG, "Google Maps API::onConnectionSuspended");
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.d(TAG, "Google Maps API::onConnectionFailed");
}