Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了没有调用android – onConnected()来获取位置更新(GooglePlayApi用于位置)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
所以我有这个代码
package com.entu.bocterapp;

import android.content.Context;
import android.LOCATIOn.LOCATIOn;
import android.LOCATIOn.LOCATIOnListener;
import android.os.bundle;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayservicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.LOCATIOn.LOCATIOnrequest;
import com.google.android.gms.LOCATIOn.LOCATIOnservices;

public class LOCATIOnManager implements GoogleApiClient.ConnectionCallBACks,GoogleApiClient.onConnectionFailedListener {

private Context mContext;
private GoogleApiClient mGoogleApiClient;
private LOCATIOn mLastLOCATIOn;
private LOCATIOnrequest mLOCATIOnrequest;

public LOCATIOnManager(Context context) {
    mContext = context;
    //
    if (checkIfGooglePlayservicesAreAvailable()) {
        //Get Access to the google service api
        buildGoogleApiClient();
        mGoogleApiClient.connect();
    } else {
        //Use Android LOCATIOn services
        //TODO:
    }
}

public LOCATIOn getCoarseLOCATIOn() {
    if (mLastLOCATIOn != null) {
        return mLastLOCATIOn;
    } else return null;
}

private synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.builder(mContext)
            .addConnectionCallBACks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LOCATIOnservices.API)
            .build();
}

private Boolean checkIfGooglePlayservicesAreAvailable() {
    int errorCode = GooglePlayservicesUtil.isGooglePlayservicesAvailable(mContext);
    if (errorCode != ConnectionResult.succesS) {
        GooglePlayservicesUtil.getErrorDialog(errorCode,(RecentSighTings) mContext,0).show();
        return false;
    }
    return true;
}


@Override
public void onConnected(Bundle bundlE) {
    LOCATIOn LOCATIOn = LOCATIOnservices.FusedLOCATIOnApi.getLastLOCATIOn(mGoogleApiClient);
    if (LOCATIOn != null) {
        mLastLOCATIOn = LOCATIOn;
        Toast.makeText(mContext,LOCATIOn.getLongitude() + "," + LOCATIOn.getLatitude() + " : " + LOCATIOn.getAccuracy(),Toast.LENGTH_LONG).show();
    }
}

@Override
public void onConnectionSuspended(int i) {
    Toast.makeText(mContext,"suspended",Toast.LENGTH_LONG).show();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

}

我想在这个类中获取位置,但onConnected()永远不会被调用(我等了1-2分钟).我使用调试器,它说谷歌播放服务可用.

有谁知道我做错了什么?我被困在这里几个小时,阅读一切,无法让它工作.

干杯!

解决方法

你必须打电话 @H_119_2@mGoogleApiClient.connect();

大佬总结

以上是大佬教程为你收集整理的没有调用android – onConnected()来获取位置更新(GooglePlayApi用于位置)全部内容,希望文章能够帮你解决没有调用android – onConnected()来获取位置更新(GooglePlayApi用于位置)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。