无法从播放服务获取位置更新
Unable to get location updates from play services
我正在尝试从我的 android 应用程序上的 google 播放服务请求位置更新。到目前为止,如果我先启动 google 地图,我可以获得最后的位置,但无法获得任何更新。
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();
}
}
当我在使用 google 地图后启动应用程序时,我从以下位置获取最后缓存的位置:
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
但紧接着,我打印出定位服务不可用:
LocationAvailability a = LocationServices.FusedLocationApi.getLocationAvailability(mGoogleApiClient);
System.out.println(a);
onLocationChanged/onConnectionFailed/onConnectionSuspended 从未被调用。
我 运行 我的应用程序在虚拟 Nexus 6 上运行 android 5.1.0 在 Genymotion 上。
我认为您在这里错过了一些事情。在你的 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 确保它可以接收位置
我正在尝试从我的 android 应用程序上的 google 播放服务请求位置更新。到目前为止,如果我先启动 google 地图,我可以获得最后的位置,但无法获得任何更新。
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();
}
}
当我在使用 google 地图后启动应用程序时,我从以下位置获取最后缓存的位置:
LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
但紧接着,我打印出定位服务不可用:
LocationAvailability a = LocationServices.FusedLocationApi.getLocationAvailability(mGoogleApiClient);
System.out.println(a);
onLocationChanged/onConnectionFailed/onConnectionSuspended 从未被调用。
我 运行 我的应用程序在虚拟 Nexus 6 上运行 android 5.1.0 在 Genymotion 上。
我认为您在这里错过了一些事情。在你的 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 确保它可以接收位置