我的应用只需要知道用户所在的国家/地区,而我的应用并不关心用户的移动位置。
我只想知道:当用户启动应用程序时,应用程序会将用户的大致位置(纬度和经度)发送到服务器。
这是我的错误代码。我尝试向getlocation函数发送一个字符串,我希望它可以返回原始字符串并添加纬度和经度信息。
private String getlocation (String url){
LocationManager status = (LocationManager) (this.getSystemService(Context.LOCATION_SERVICE));
if (status.isProviderEnabled(LocationManager.GPS_PROVIDER) || status.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Log.v("print","enable to locate");
} else {
Log.v("print","fail to locate");
}
Location location = status.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location!=null){
Log.v("print","succed!!!@@");
Double longitude = location.getLongitude();
String loc=Double.toString(longitude);
url=url+loc;
}
else{
Log.v("print","location return null");
}
return url;
}
我使用network_provider,它总是打印“启用定位”,然后打印“位置返回空”
那么我的代码有什么问题,我的位置总是返回 null。我使用 AVD manerger,我也尝试使用 GPS_PROVIDER。我使用模拟器控制在位置控制上发送十进制。它也不起作用。
顺便说一句,我也设置了网络和 GPS 权限。
非常感谢您的帮助!