android - onConnected() 未被调用以获取位置更新(用于位置的 GooglePlayApi)

标签 android location google-play-services

所以我有这段代码:

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 分钟)。我使用了调试器,它说 google play 服务可用。

有谁知道我做错了什么?我被困在这里几个小时,阅读所有内容,但无法正常工作。

干杯!

最佳答案

你必须打电话

mGoogleApiClient.connect();

关于android - onConnected() 未被调用以获取位置更新(用于位置的 GooglePlayApi),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28301273/

相关文章:

java - Android 如何从我的主要 Activity 中调用 fragment

java - 资源$NotFoundException

facebook - 国家/地区限制 - Facebook 在过滤门控内容时如何确定用户的位置?

android - 如何在Android Studios中获取附近的Wifi距离和方向

android - 从 android 应用程序重定向到 playstore 市场 url 不起作用

google-play-services - com.android.tools.r8.utils.AbortException zza 已经有一个 mappin

Android updateAppWidget 不更新

android - 在两个 Pane 布局的工具栏中定位菜单项

java - 位置返回 NULL

android - android maps api V2 是否需要启用后台数据?