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

如何解决如何使用 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.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?