Android的lastKnownLocation返回null [英] Android lastKnownLocation returns null

查看:362
本文介绍了Android的lastKnownLocation返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题,我看不出有什么错在code,但getLastKnownLocation空每次都返回。任何想法?

I've got this problem I don't see anything wrong in the code but getLastKnownLocation returns null every time . any ideas ?

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");

    }
}

感谢

推荐答案

getLastKnownLocation()经常会返回 ,尤其是当所述的位置信息提供(例如,GPS)的最近未使用。你只用 getLastKnownLocation()的情况下,你要么不真正的需求的位置(但想有一个),或在那里你会使用其他技术如 getLastKnownLocation()收益(例如,请求位置更新)。

getLastKnownLocation() will frequently return null, particularly if the location provider (e.g., GPS) has not been used recently. You only use getLastKnownLocation() in situations where you either do not really need a location (but would like to have one) or where you will use other techniques if getLastKnownLocation() returns null (e.g., request location updates).

这篇关于Android的lastKnownLocation返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆