gpt4 book ai didi

java - 无法从播放服务获取位置更新

转载 作者:行者123 更新时间:2023-11-29 20:09:49 25 4
gpt4 key购买 nike

我正在尝试从我的 Android 应用程序上的 Google Play 服务请求位置更新。到目前为止,如果我先启动谷歌地图,我可以获得最后的位置,但无法获得任何更新。

public class Inbox extends AppCompatActivity implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
com.google.android.gms.location.LocationListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

int r = GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(this);
if(r != 0){
System.out.println(r);
Dialog d = GoogleApiAvailability.getInstance().getErrorDialog(this,r,1);
d.show();
}
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.addApi(AppIndex.API).build();
}
mGoogleApiClient.connect();
super.onStart();
}

@Override
public void onConnected(Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
System.out.println("Problem with rights");
}
else{
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
LocationRequest mLocationRequest = new LocationRequest();

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

if(mLastLocation != null){
System.out.println(mLastLocation.toString());
}
else {
System.out.println("No location cached");
}
LocationAvailability a = LocationServices.FusedLocationApi.getLocationAvailability(mGoogleApiClient);
System.out.println(a);
}
}


@Override
public void onConnectionSuspended(int i) {
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
System.out.println(location.toString());
}




@Override
public void onStart() {
super.onStart();
mGoogleApiClient.connect();
Action viewAction = Action.newAction(...);
AppIndex.AppIndexApi.start(mGoogleApiClient, viewAction);
}

@Override
public void onStop() {
super.onStop();
Action viewAction = Action.newAction(...);
AppIndex.AppIndexApi.end(mGoogleApiClient, viewAction);
mGoogleApiClient.disconnect();
}

}

当我在使用谷歌地图后启动应用程序时,我从以下位置获取最后缓存的位置:

LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

但紧接着,我打印出位置服务不可用:

LocationAvailability a = LocationServices.FusedLocationApi.getLocationAvailability(mGoogleApiClient);
System.out.println(a);

永远不会调用 onLocationChanged/onConnectionFailed/onConnectionSuspended。

我在虚拟 Nexus 6 上运行我的应用程序,在 Genymotion 上安装了 android 5.1.0。

最佳答案

我认为您在这里错过了一些事情。在你的 onCennected 回调中,你必须首先检查你是否可以获得最后已知的位置。如果它不可用,则您必须请求位置更新。如下所示重构您的 onConnected,它应该可以正常工作

@Override
public void onConnected(Bundle bundle) {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
System.out.println("Problem with rights");
} else {

Location lastLocation = LocationServices.FusedLocationApi
.getLastLocation(mGoogleApiClient);

if (lastLocation != null) {
//you have a location. use that here. no need to request for location
// updates
mGoogleApiClient.disconnect();
} else {
//you have to request for location
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(2000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setSmallestDisplacement(50);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi
.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

}

}
}

然后在您的 locationChanged 回调中执行以下操作

@Override
public void onLocationChanged(Location location) {

//now that we got the initial location. it is time to stop the api client
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();

// use your newly obtained location here

}

注意:如果您在 GenyMotion 上尝试这个。通过在 GM 模拟器上启用 GPS 确保它可以接收位置

关于java - 无法从播放服务获取位置更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35172055/

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