【发布时间】:2018-04-15 22:50:20
【问题描述】:
当我点击切换按钮时,我正在使用这个What is the simplest and most robust way to get the user's current location on Android? 获取我的当前位置并列出附近的餐馆。它在我删除应用程序并重新安装之前工作。 这是我的 gotLocation 方法的实现
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
MyLocationManager.LocationResult locationResult= new MyLocationManager.LocationResult() {
@Override
public void gotLocation(final Location location) {
//Got the location!
currentLocation = location;
browseNearbyBusinessesList(currentLocation);
}
};
MyLocationManager myLocationManager = new MyLocationManager();
myLocationManager.getLocation(getBaseContext(), locationResult);
}else {
searchDefaultList();
}
}
});
我得到的不是空的 LocationResult 对象,但是 gotLocation 方法没有被执行。谁能告诉我问题到底出在哪里?
这是 MyLocationManager 类的代码
public class MyLocationManager {
public LocationResult locationResult;
public Context context;
Timer timer1;
LocationManager lm;
boolean gps_enabled = false;
boolean network_enabled = false;
LocationListener locationListenerGps = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerNetwork);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
timer1.cancel();
locationResult.gotLocation(location);
lm.removeUpdates(this);
lm.removeUpdates(locationListenerGps);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
private boolean isPermessed = false;
public MyLocationManager() {
}
public MyLocationManager(boolean isPermessed) {
this.isPermessed = isPermessed;
}
public boolean getLocation(Context context, LocationResult result) {
//I use LocationResult callback class to pass location value from MyLocation to user code.
this.context = context;
locationResult = result;
if (lm == null)
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
//exceptions will be thrown if provider is not permitted.
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
}
try {
network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
} catch (Exception ex) {
}
//don't start listeners if no provider is enabled
if (!gps_enabled && !network_enabled)
return false;
if (gps_enabled) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
isPermessed = false;
return false;
} else {
isPermessed = true;
}
if (isPermessed)
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
}
if (network_enabled) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
isPermessed = false;
return false;
} else {
isPermessed = true;
}
if (isPermessed)
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
}
timer1 = new Timer();
timer1.schedule(new GetLastLocation(), 4000);
return true;
}
public static abstract class LocationResult {
public abstract void gotLocation(final Location location);
}
class GetLastLocation extends TimerTask {
@Override
public void run() {
lm.removeUpdates(locationListenerGps);
lm.removeUpdates(locationListenerNetwork);
Location net_loc = null, gps_loc = null;
if (gps_enabled) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
isPermessed = false;
return;
} else {
isPermessed = true;
}
if (isPermessed)
gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
if (network_enabled) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
isPermessed = false;
return;
} else {
isPermessed = true;
}
if (isPermessed)
net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
System.out.println(isPermessed);
//if there are both values use the latest one
if (gps_loc != null && net_loc != null) {
if (gps_loc.getTime() > net_loc.getTime())
locationResult.gotLocation(gps_loc);
else
locationResult.gotLocation(net_loc);
return;
}
if (gps_loc != null) {
locationResult.gotLocation(gps_loc);
return;
}
if (net_loc != null) {
locationResult.gotLocation(net_loc);
return;
}
locationResult.gotLocation(null);
}
}
}
【问题讨论】:
-
请发布您的 MyLocalionManager 代码
-
完成,上面链接中的代码仍然相同。
-
请将此代码:ex.printStackTrace() 添加到您捕获的所有内容中(Exception ex)。请永远不要让它为空,您将不会获得任何信息并因此而花费数小时。稍后,请在 lm.requestLocationUpdates 之前添加一个日志,以查看是否被调用。在继续之前让我们看看逻辑
-
解决了!从未授予权限,谢谢帮助。
-
很高兴听到这个消息:)
标签: android google-location-services