LocationManager requestLocationUpdates不起作用

Dan*_*inu 5 android geolocation locationmanager

我正在尝试使用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);
    }
}
Run Code Online (Sandbox Code Playgroud)

和我的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) {}
}
Run Code Online (Sandbox Code Playgroud)

因此永远不会调用onLocationChanged.谁能看到问题?非常感谢

Dev*_*red 1

确保在“设置”中启用了正确的定位服务。您还可以用来isProviderEnabled()检查您选择的特定提供商是否有效。并确保您在清单中具有粗略或精细位置权限。

另外,请确保您正在设备上进行测试。网络定位在模拟器中根本不起作用,只有使用模拟工具注入新的数据点时,GPS 才会起作用。