Android:GoogleApiClient 还没有连接?
Android:GoogleApiClient is not connected yet?
更新用户的当前位置
我在我的项目中实现了以下方法:
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener
项目执行时,会出现如下错误:
java.lang.IllegalStateException: GoogleApiClient is not connected yet.
并且代码有错误:
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
提出了很多方案,赞This and
但没有解决任何问题。
请帮忙解决
我在 onCreate 方法中的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
FirebaseCrash.report(new Exception("Exceptin"));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mGoogleClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
.setInterval(60*1000)
.setFastestInterval(1*1000);}
并在 onMapReady 中:
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
buildGoogleApiClient();
mMap.setTrafficEnabled(true);
mMap.setMyLocationEnabled(true);}
还有这个:
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if (servicesAvailable()) {
// Get best last location measurement meeting criteria
mLastLocation = bestLastKnownLocation(MIN_LAST_READ_ACCURACY, FIVE_MIN);
if (null == mLastLocation
|| mLastLocation.getAccuracy() > MIN_LAST_READ_ACCURACY
|| mLastLocation.getTime() < System.currentTimeMillis() - TWO_MIN) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
// Schedule a runnable to unregister location listeners
Executors.newScheduledThreadPool(1).schedule(new Runnable() {
@Override
public void run() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, MainActivity.this);//This Line Return Error
}
}, ONE_MIN, TimeUnit.MILLISECONDS);
}
}else{
handleNewLocation(mLastLocation);
}
}
感谢您的帮助:)
这是适合我的完整实现..
public class MainActivity extends AppCompatActivity implements
GoogleApiClient.OnConnectionFailedListener,
GoogleApiClient.ConnectionCallbacks,
LocationListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buildGoogleApiClient();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.d(TAG, "Connection established. Fetching location ..");
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onLocationChanged(Location location) {
Log.d("Lat: " + location.getLatitude() + "Lng : " + location.getLongitude());
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
}
public synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(getBaseContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onStart(){
super.onStart();
if(this.googleApiClient != null){
this.googleApiClient.connect();
}
}
@Override
public void onPause() {
super.onPause();
//stop location updates when Activity is no longer active
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
}
在 onCreate 中,您在没有客户端的情况下调用 getMapAsync()
。
您需要在 onCreate
中调用 mGoogleClient.connect()
,然后将您的 mapFragment.getMapAsync(this)
移动到 onConnected
.
解决了我的问题,因为我在我的项目中使用了闪屏,将这段代码放在闪屏中 activity :
mGoogleClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
更新用户的当前位置 我在我的项目中实现了以下方法:
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
项目执行时,会出现如下错误:
java.lang.IllegalStateException: GoogleApiClient is not connected yet.
并且代码有错误:
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
提出了很多方案,赞This and
请帮忙解决
我在 onCreate 方法中的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
FirebaseCrash.report(new Exception("Exceptin"));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mGoogleClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
.setInterval(60*1000)
.setFastestInterval(1*1000);}
并在 onMapReady 中:
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
buildGoogleApiClient();
mMap.setTrafficEnabled(true);
mMap.setMyLocationEnabled(true);}
还有这个:
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
if (servicesAvailable()) {
// Get best last location measurement meeting criteria
mLastLocation = bestLastKnownLocation(MIN_LAST_READ_ACCURACY, FIVE_MIN);
if (null == mLastLocation
|| mLastLocation.getAccuracy() > MIN_LAST_READ_ACCURACY
|| mLastLocation.getTime() < System.currentTimeMillis() - TWO_MIN) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
// Schedule a runnable to unregister location listeners
Executors.newScheduledThreadPool(1).schedule(new Runnable() {
@Override
public void run() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, MainActivity.this);//This Line Return Error
}
}, ONE_MIN, TimeUnit.MILLISECONDS);
}
}else{
handleNewLocation(mLastLocation);
}
}
感谢您的帮助:)
这是适合我的完整实现..
public class MainActivity extends AppCompatActivity implements
GoogleApiClient.OnConnectionFailedListener,
GoogleApiClient.ConnectionCallbacks,
LocationListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buildGoogleApiClient();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.d(TAG, "Connection established. Fetching location ..");
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onLocationChanged(Location location) {
Log.d("Lat: " + location.getLatitude() + "Lng : " + location.getLongitude());
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
}
public synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(getBaseContext())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onStart(){
super.onStart();
if(this.googleApiClient != null){
this.googleApiClient.connect();
}
}
@Override
public void onPause() {
super.onPause();
//stop location updates when Activity is no longer active
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
}
在 onCreate 中,您在没有客户端的情况下调用 getMapAsync()
。
您需要在 onCreate
中调用 mGoogleClient.connect()
,然后将您的 mapFragment.getMapAsync(this)
移动到 onConnected
.
解决了我的问题,因为我在我的项目中使用了闪屏,将这段代码放在闪屏中 activity :
mGoogleClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();