如何使用 GoogleApi 接口替换已弃用的 GoogleApiClient?

问题描述

我正在尝试学习我正在开发的应用的地理围栏教程。下面是 GeolocationService 的部分代码,但它使用的是已弃用的 Googleapiclient。我一直在阅读 this 文档,其中建议更改为 GoogleApi 界面。我一直无法弄清楚事情。我的问题似乎与“buildGoogleapiclient方法有关。我似乎无法理解将 GoogleApi 接口用于 LocationServices 的正确语法。我们将不胜感激任何有关如何实现此目标(或实现此目标的更好方法)的帮助、建议或示例。

public class GeolocationService extends Service implements ConnectionCallbacks,OnConnectionFailedListener,LocationListener,ResultCallback<Status> {
    public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10000;
    public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 5;
    protected Googleapiclient mGoogleapiclient;
    protected LocationRequest mLocationRequest;

    private PendingIntent mPendingIntent;

    @Override
    public void onStart(Intent intent,int startId) {
        buildGoogleapiclient();

        mGoogleapiclient.connect();
    }

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

        if (mGoogleapiclient.isConnected()) {
            mGoogleapiclient.disconnect();
        }

    }

    protected void registerGeofences() {
        if (MainActivity.geofencesAlreadyRegistered) {
            return;
        }

        Log.d("Geofence","Registering Geofences");

        HashMap<String,SimpleGeofence> geofences = SimpleGeofenceStore
                .getInstance().getSimpleGeofences();

        GeofencingRequest.Builder geofencingRequestBuilder = new GeofencingRequest.Builder();
        for (Map.Entry<String,SimpleGeofence> item : geofences.entrySet()) {
            SimpleGeofence sg = item.getValue();

            geofencingRequestBuilder.addGeofence(sg.toGeofence());
        }

        GeofencingRequest geofencingRequest = geofencingRequestBuilder.build();

        mPendingIntent = requestPendingIntent();

        **LocationServices.GeofencingApi.addGeofences(mGoogleapiclient,geofencingRequest,mPendingIntent).setResultCallback(this);**

        MainActivity.geofencesAlreadyRegistered = true;
    }

    private PendingIntent requestPendingIntent() {

        if (null != mPendingIntent) {

            return mPendingIntent;
        } else {

            Intent intent = new Intent(this,GeofenceReceiver.class);
            return PendingIntent.getService(this,intent,PendingIntent.FLAG_UPDATE_CURRENT);

        }
    }

    public void broadcastLocationFound(Location location) {
        Intent intent = new Intent("geolocation.service");
        intent.putExtra("latitude",location.getLatitude());
        intent.putExtra("longitude",location.getLongitude());
        intent.putExtra("done",1);

        sendbroadcast(intent);
    }

    **protected void startLocationUpdates() {
        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleapiclient,mLocationRequest,this);
    }**

    protected void stopLocationUpdates() {
        LocationServices.FusedLocationApi.removeLocationUpdates(
                mGoogleapiclient,this);
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        Log.i("Geofence","Connected to Googleapiclient");
        **startLocationUpdates();**
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.d("Geofence","new location : " + location.getLatitude() + ","
                        + location.getLongitude() + ". "
                        + location.getAccuracy());
        broadcastLocationFound(location);

        if (!MainActivity.geofencesAlreadyRegistered) {
            registerGeofences();
        }
    }

    @Override
    public void onConnectionSuspended(int cause) {
        Log.i("Geofence","Connection suspended");
        mGoogleapiclient.connect();
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.i("Geofence","Connection Failed: ConnectionResult.getErrorCode() = "
                        + result.getErrorCode());
    }

    *protected synchronized void buildGoogleapiclient() {
        Log.i("Geofence","Building Googleapiclient");
        mGoogleapiclient = new Googleapiclient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API).build();*
        createLocationRequest();
    }

    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
        mLocationRequest
                .setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public void onResult(Status status) {
        if (status.isSuccess()) {
            Toast.makeText(getApplicationContext(),getString(R.string.geofences_added),Toast.LENGTH_SHORT)
                    .show();
        } else {
            MainActivity.geofencesAlreadyRegistered = false;
            String errorMessage = getErrorString(this,status.getStatusCode());
            Toast.makeText(getApplicationContext(),errorMessage,Toast.LENGTH_LONG).show();
        }
    }

    public static String getErrorString(Context context,int errorCode) {
        Resources mResources = context.getResources();
        switch (errorCode) {
            case GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE:
                return mResources.getString(R.string.geofence_not_available);
            case GeofenceStatusCodes.GEOFENCE_TOO_MANY_GEOFENCES:
                return mResources.getString(R.string.geofence_too_many_geofences);
            case GeofenceStatusCodes.GEOFENCE_TOO_MANY_PENDING_INTENTS:
                return mResources
                        .getString(R.string.geofence_too_many_pending_intents);
            default:
                return mResources.getString(R.string.unkNown_geofence_error);
        }
    }
}```


  [1]: https://android-developers.googleblog.com/2017/11/moving-past-googleapiclient_21.html

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...