在我的应用程序中,我正在使用获取当前位置。在这方面,我已经开发了一些代码,您可以在下面看到它。
public class MyGPS extends Service implements LocationListener
{
protected LocationManager locationManager;
private final Context mContext;
public MyGPS (Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
// Check network status
if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Log.e("Location ", "Network enabled");
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1000,1, this);
if (locationManager != null)
{
Log.e("Location ", "Location manager is not null");
Location networkLocation= locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (networkLocation != null) {
return networkLocation;
}
else
{
Log.e("Location ", "networkLocation object is null");
}
}
}
}
catch(Exception e) {
Log.e("Location ", "Problum in getLocation()");
}
return null;
}
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}通过运行上面的代码,它显示了调试消息Log.e("Location ", "networkLocation object is null");
现在我的问题是为什么networkLocation对象是空的。请在这方面帮助我。提前谢谢。
发布于 2013-11-12 09:36:22
你设定许可了吗?清单使用-权限android:name="android.permission.ACCESS_FINE_LOCATION“清单
http://developer.android.com/guide/topics/location/strategies.html
发布于 2013-11-12 09:31:57
当设备获得位置时,它将调用onLocationChanged侦听器。在上述情况下,您正在尝试获取lastKnownLocation。如果没有lastKnownLocation,它将作为null返回。
因此,请从onLocationChanged侦听器中保存位置。
发布于 2013-11-12 09:34:42
在API级别1中添加的公共位置getLastKnownLocation (字符串提供程序) 返回一个位置,该位置指示从给定提供程序获得的上一次已知位置修复的数据。 这可以在不启动提供程序的情况下完成。请注意,此位置可能已过时,例如,如果设备被关闭并移动到另一个位置。 如果当前禁用了提供程序,则返回null。参数提供程序提供程序的名称返回 提供程序的最后一个已知位置,或null。
https://stackoverflow.com/questions/19925391
复制相似问题