gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:30:26 26 4
gpt4 key购买 nike

所以我有这段代码:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com