【问题标题】:Android lastKnownLocation returns nullAndroid lastKnownLocation 返回 null
【发布时间】:2012-09-02 17:42:40
【问题描述】:

我遇到了这个问题,我在代码中看不到任何错误,但 getLastKnownLocation 每次都返回 null 。有什么想法吗?

public class LocationDemo2Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        EditText et1 = (EditText) findViewById(R.id.editText1);

        LocationManager manager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if(location != null)    et1.setText((int)location.getLatitude());
        else et1.setText("null");

    }
}

谢谢

【问题讨论】:

    标签: android geolocation location


    【解决方案1】:

    getLastKnownLocation() 将经常返回null,尤其是在最近未使用位置提供程序(例如 GPS)的情况下。您只在以下情况下使用getLastKnownLocation():您真的需要一个位置(但想要一个),或者如果getLastKnownLocation() 返回null(例如,请求位置更新)。

    【讨论】:

    • 我如何找到位置??我可以强制操作系统使用 GPS 或网络或 Wifi 来查找当前位置
    • 我如何以编程方式打开谷歌的定位服务我试过这个stackoverflow.com/questions/4721449/… 但它不起作用我在 2.3.3 gingebread 中需要它
    • @user2726456:使用requestLocationUpdates() 请求位置更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-22
    • 2012-05-28
    • 2014-12-05
    • 2021-07-07
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多