getLastKnownLocations总是返回null
问题描述:
我无法在我的Android设备(Samsung Galaxy Note N7000)上获得最终识别位置,因为它始终为空,塔被检测为网络,所以没关系,任何人都知道如何获取我的位置?如果该位置已被其他应用程序拨打电话前,确定getLastKnownLocations总是返回null
d = getResources().getDrawable(R.drawable.songs_gray);
//Placing pinpoint at location
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
towers = lm.getBestProvider(crit, false);
Toast.makeText(LocationActivity.this, "Towers: " + towers, Toast.LENGTH_SHORT).show();
Location location = lm.getLastKnownLocation(towers);
Toast.makeText(LocationActivity.this, "Location: " + location, Toast.LENGTH_SHORT).show();
if(location !=null){
lat = (int) (location.getLatitude()*1E6);
longi = (int) (location.getLongitude()*1E6);
GeoPoint ourLocation = new GeoPoint(lat, longi);
OverlayItem overlayItem = new OverlayItem(ourLocation, "What's up", "2nd String");
CustomPinpoint custom = new CustomPinpoint(d, LocationActivity.this);
custom.insertPinPoint(overlayItem);
overlayList.add(custom);
}else{
Toast.makeText(LocationActivity.this, "Couldn't get provider", Toast.LENGTH_SHORT).show();
}
清单
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
答
最后已知的位置才可用。启动地图或其他应用程序,并让它获得位置锁定。然后运行你的应用。
如果您真的需要需要位置,您需要回退到其他某种方法,因为您不能依赖存在最后一个已知位置。
作为附注,ACCESS_MOCK_LOCATION
在这里不是必需的。从文档,
允许应用程序用于测试
答
随着getLastKnownLocation()
你只能得到一个Location
对象时指定的位置提供激活创建模拟位置提供。
文档说:
返回一个位置指示从给定的提供弄不好获得从最后已知位置 数据。这可以在没有 启动提供程序的情况下完成。请注意,如果设备已关闭并移动到另一个 位置,则该位置可能已过时,例如 。
如果提供者当前被禁用,则返回null。
要检测当前位置而不是最后检测到的位置,您需要启用位置提供程序。 This tutorial应该可以帮助您了解如何与位置服务商一起使用GPS和网络。
答
不知道我的问题!只需在我的手机上的Google定位服务
谷歌地图应用程序如何知道我的位置,但此命令仍然返回null? – 2013-05-07 19:11:39