2015年3月7日土曜日

Change LocationClient to GoogleApiClient

Enviroment

  • android:minSdkVersion="14"
  • android:targetSdkVersion="19"
  • com.google.android.gms:play-services:6.5.+
  • android studio

In my project, I changed play-services version6.1. to play-services 6.5.+.
But, Building Errors occurred.

Unfortunately, LocationClient came to deprecated class. Instead, GoogleApiClient class was released.

As I modified out-of-date code, I open to public latest code.
Please make good use of this code.

Attention: Under Sample Code is not Activity, but Service.

Sample Code


import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.eastcore.cargo.BuildConfig;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;

/**
 * LocationService
 * 位置情報を取得し続けるだけのサービスクラス
 */
public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {

    public static final String TAG = "LocationService";

    //protected LocationClient mLocationClient;
    protected GoogleApiClient mGoogleApiClient;

    protected LocationRequest mLocationRequest;

    // 緯度
    protected double mLatitude;
    // 経度
    protected double mLongitude;
    // 標高
    protected double mAltitude;

    @Override
    public void onCreate() {
        super.onCreate();
        if (isLocationEnabled()) {
            if (BuildConfig.DEBUG) {
                Log.d(TAG, "-----onConnected----");
            }
            connectGooglePlayServices();
        }
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy");
        super.onDestroy();
        disConnectGooglePlayServices();
    }

    /**
     * 現在地情報が取得可能な場合はtrue, 取得できない場合はfalse
     * @return
     */
    protected boolean isLocationEnabled() {
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    /**
     * GooglePlayServicesに接続する
     */
    protected void connectGooglePlayServices() {

        mLocationRequest = LocationRequest.create();
        // 10秒おきに位置情報を取得する
        mLocationRequest.setInterval(10000);
        // 精度優先
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        // 最短で5秒おきに位置情報を取得する
        // mLocationRequest.setFastestInterval(5000);

//        mLocationClient = new LocationClient(getActivity().getApplicationContext(), connectionCallbacks, onConnectionFailedListener);
//        mLocationClient.connect();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        if (mGoogleApiClient != null) {
            // GoogleApiClient start
            mGoogleApiClient.connect();
        }

    }

    /**
     * GooglePlayServicesを切断する
     */
    protected void disConnectGooglePlayServices() {

        if (mGoogleApiClient.isConnected()) {
            // 位置情報の取得を停止
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
            //mGoogleApiClient.removeLocationUpdates(locationListener);
            mGoogleApiClient.disconnect();
        }

    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onConnected(Bundle bundle) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

    }

    @Override
    public void onConnectionSuspended(int i) {
    }

    @Override
    public void onLocationChanged(Location location) {
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        double altitude = location.getAltitude();

        // latitude changed
        if (mLatitude != latitude) {
            mLatitude = latitude;
        }

        // longitude changed
        if (mLongitude != longitude) {
            mLongitude = longitude;
        }

        // altitude changed
        if (mAltitude != altitude) {
            mAltitude = altitude;
        }

        if (BuildConfig.DEBUG) {
            Log.d(TAG, "mLatitude:" + mLatitude + ", mLongitude: " + mLongitude + ", mAltitude: " + mAltitude);
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {}
}

It is easy to change out-of-date code.

if your code still have deprecated class, it should be replaced the latest code.

thanks.

Reference Sites

この記事がお役にたちましたらシェアをお願いします

このエントリーをはてなブックマークに追加

0 件のコメント:

コメントを投稿

Related Posts Plugin for WordPress, Blogger...