如果手机上的所有内容都已关闭,我的位置有问题,那样我应该显示该用户应该启用GPS,但问题是我应该在哪里调用对话框的显示?
这是我的代码:
Criteria c = new Criteria();
location = locationManager.getLastKnownLocation(locationManager.getBestProvider(c, false));
Log.v("--", "provider " + locationManager.getBestProvider(c, false));
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 1000, 1000, mLocationListener);
}
事情是有时if(location == null)
为真且locationManager.requestLocationUpdates
没有执行。我该如何检查它是否执行?我的猜测是我应该在这里显示启用GPS对话框?有人可以给我一个指南吗?
答案 0 :(得分:0)
当用户点击地图上的myLocation按钮时,我会亲自检查位置服务是否已启用:
googleMap.setOnMyLocationButtonClickListener(new OnMyLocationButtonClickListener() {
@Override
public boolean onMyLocationButtonClick() {
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabledGPS = service
.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean enabledWiFi = service
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!enabledGPS) {
Toast.makeText(getApplicationContext(),
"GPS signal not found", Toast.LENGTH_LONG)
.show();
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
} else if (!enabledWiFi) {
Toast.makeText(getApplicationContext(),
"Network signal not found",
Toast.LENGTH_LONG).show();
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
return false;
}
});
是你想要的吗?
答案 1 :(得分:0)
当您调用requestLocationUpdates(...)时,还要创建一个在位置更新后稍后过期的计时器,以便您可以检查是否已调用位置更新:
private static boolean hasUpdateHappened = false;
...
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1000, mLocationListener);
// mLocationListener should set hasUpdateHappened to true
...
myView.postDelayed(new Runnable() {
@Override
public void run() {
if (!hasUpdateHappened){
//TODO Show dialog
}
}
}, 2000);