问题描述
我有这个问题,我看不出有什么错在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!