我要从 NETWORK_PROVIDER中获得gps坐标,用的以下的代码实现这个功能。
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
//init();
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
//_pd.dismiss();
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " +"Latitud = " + loc.getLatitude() +"Longitud =" + loc.getLongitude();
txtInfo.setText(Text);
Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
但是,使用上面的方法有时候能获得gps坐标,有时候又获取不了,为什么呢?