我正在尝试使用 LocationManager 和 LocationListener 在 android 中获取当前位置,如http://developer.android.com/guide/topics/location/obtaining-user-location.html中所述
但是,永远不会调用 LocationListener 的 onLocationChanged 方法。我使用了真正的 android 手机/还使用了模拟器和使用 telnet 更改的模拟位置,如上面的链接中所述。
这是我的代码:
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
LocationManager mlocManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new CustomLocationListener(
getApplicationContext());
// Location Providers
String locationProvider = LocationManager.NETWORK_PROVIDER;
// LocationProvider locationProvider = LocationManager.GPS_PROVIDER;
mlocManager
.requestLocationUpdates(locationProvider, 0, 0, mlocListener);
}
}
和我的 LocationListener:
public class CustomLocationListener implements LocationListener {
private Context m_context;
public CustomLocationListener(Context context) {
m_context = context;
}
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
String Text = latitude + " " + longitude;
Toast.makeText(m_context, Text, Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
// TODO
}
@Override
public void onProviderEnabled(String provider) {
// TODO
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {}
}
所以永远不会调用 onLocationChanged。任何人都可以看到问题吗?非常感谢