在我的应用程序中,我正在使用网络提供商获取当前位置。在这方面我已经开发了一些代码,你可以在下面看到它。
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
对象为空。请在这方面帮助我。提前致谢。