通过eclipse重新安装apk文件后,getlastknownlocation总是返回null
Posted
技术标签:
【中文标题】通过eclipse重新安装apk文件后,getlastknownlocation总是返回null【英文标题】:getlastknownlocation always return null after I re-install the apk file via eclipse 【发布时间】:2013-04-06 11:58:31 【问题描述】:最近,我创建了一个简单的应用程序来获取 gps 位置并在 android 手机上显示。 刚开始尝试几次后我就可以获取位置,但是在我重新安装 apk 文件后,getLastKnownLocation() 总是返回一个空值。
使用的开发环境: - API 10 姜饼 2.3.6 - 使用 GPS 提供程序
下面是我在我的android项目中应用的代码:
public class MyActivity extends MapActivity
protected void onCreate(Bundle savedInstanceState)
mapView = (MapView)findViewById(R.id.myTripMap);
mapController = mapView.getController();
mapView.setSatellite(false);
mapView.setStreetView(true);
mapView.displayZoomControls(false);
mapView.setBuiltInZoomControls(true);//
mapView.setClickable(true);
mapController.setZoom(14);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
provider = locationManager.getBestProvider(criteria, true);
location = locationManager.getLastKnownLocation(provider);
updateMyCurrentLoc(location);
locationManager.requestLocationUpdates(provider, 2, 1,locationListener);
private final LocationListener locationListener = new LocationListener()
public void onLocationChanged(Location location)
updateMyCurrentLoc(location);
public void onProviderDisabled(String provider)
updateMyCurrentLoc(null);
public void onProviderEnabled(String provider)
public void onStatusChanged(String provider, int status,
Bundle extras)
;
private void updateMyCurrentLoc(Location location)
if (location != null)
// other codes to get the address and display
Toast.makeText(getBaseContext(), "provider used : "+provider).show(); //testing purpose
else
str = "No location found";
Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show();
谁能提出一个可能的解决方案来解决 getLastKnownLocation() 返回的空值? 任何帮助将不胜感激。谢谢。
【问题讨论】:
【参考方案1】:getLastKnownLocation()
这将返回最后一个已知位置...在您重新安装应用程序后,它将没有任何最后一个已知位置...所以它会导致 NPE...而不是使用下面的代码
public Location getLocation()
try
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled)
// no network provider is enabled
else
this.canGetLocation = true;
if (isNetworkEnabled)
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network Enabled");
if (locationManager != null)
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null)
latitude = location.getLatitude();
longitude = location.getLongitude();
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled)
if (location == null)
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS", "GPS Enabled");
if (locationManager != null)
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
latitude = location.getLatitude();
longitude = location.getLongitude();
catch (Exception e)
e.printStackTrace();
return location;
它对我有用……应该也对你有用……
【讨论】:
这不是很好的代码.. 将所有代码放在一个 try/catch 中而没有任何明确需要它的原则上是糟糕的编程。如果你真的想要一段很棒的代码,试试这个:web.archive.org/web/20100429083435/http://marakana.com/forums/… @Karan Mer 我在同一个地方使用同一个网络(沃达丰)的不同手机上得到不同的经度和纬度。在这种情况下我该怎么办 这在很多层面上都是错误的,我不知道该怎么说。首先,getLastKnownLocation 返回设备的最后已知位置,而不是应用程序。如果设备尚未跟踪位置,它将返回 null。因此,您的代码仍然可以返回 null,并且会定期返回。作为奖励,该代码中还有十几个其他错误。 在我的情况下,即使启用了 GPS,也无法从 GPS_PROVIDER 获取位置。每次从 NETWORK_PROVIDER 获取。任何想法 ??谁能帮帮我。 getLastKnownLocation 仍然返回 null!不适合我【参考方案2】:这个解决方案是我对@Karan Mer 代码的一点改进。
public Location getLocation()
int MIN_TIME_BW_UPDATES = 10000;
int MIN_DISTANCE_CHANGE_FOR_UPDATES = 10000;
try
locationManager = (LocationManager) getApplicationContext()
.getSystemService(LOCATION_SERVICE);
boolean isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isPassiveEnabled = locationManager
.isProviderEnabled(LocationManager.PASSIVE_PROVIDER);
boolean isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (isGPSEnabled || isNetworkEnabled || isPassiveEnabled)
this.canGetLocation = true;
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled && location == null)
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS", "GPS Enabled");
if (locationManager != null)
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (isPassiveEnabled && location == null)
locationManager.requestLocationUpdates(
LocationManager.PASSIVE_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network Enabled");
if (locationManager != null)
location = locationManager
.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if (isNetworkEnabled && location == null)
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network Enabled");
if (locationManager != null)
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
else
return null;
catch (Exception e)
e.printStackTrace();
享受
【讨论】:
【参考方案3】:您只需要在代码中再添加一行
if (locationManager != null)
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null)
latitude = location.getLatitude();
longitude = location.getLongitude();
else
Toast.makeText(
mContext,
"Location Null", Toast.LENGTH_SHORT).show();
【讨论】:
如果 location==null 怎么办?如何获取准确位置 等等你怎么了,我不知道你到底想回答什么。问题是在这里有人告诉他解决方案,如何摆脱 getLastKnownLocation(PROVIDER) 方法返回的这个空对象。不显示 toast 消息,没有任何建议下一步做什么。【参考方案4】:尝试以下代码
LocationManager locationManager = (LocationManager) getActivity()
.getSystemService(Context.LOCATION_SERVICE);
String locationProvider = LocationManager.NETWORK_PROVIDER;
Location lastlocation = locationManager.getLastKnownLocation(locationProvider);
String CurLat = String.valueOf(lastlocation.getLatitude());
String Curlongi= String.valueOf(lastlocation.getLongitude());
【讨论】:
不回答问题。【参考方案5】:通过 Eclipse 重新安装后,我遇到了同样的问题。 我解决了进入 Android 设置/安全/应用权限并确认应用程序的位置权限。
【讨论】:
这不是您需要在运行时要求用户授予这些权限的方式。 for more info check this以上是关于通过eclipse重新安装apk文件后,getlastknownlocation总是返回null的主要内容,如果未能解决你的问题,请参考以下文章
使用 javadecompilers.com 后重新编译 apk
将eclipse项目迁移到android studio会显示安装错误
如何将重新配置的 apk 文件从 Amazon Appstore 导入 Eclipse 以进行签名