我遇到了这个问题我在代码中没有看到任何错误,但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");
}
}
感谢
答案 0 :(得分:3)
getLastKnownLocation()
会频繁返回null
,特别是如果最近没有使用过位置提供商(例如GPS)。您只在{em>需要某个位置(但希望有位置)的情况下使用getLastKnownLocation()
,或者如果getLastKnownLocation()
返回{{},您将使用其他技术1}}(例如,请求位置更新)。