0

我遇到了这个问题,我在代码中看不到任何错误,但 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");

    }
}

谢谢

4

1 回答 1

3

getLastKnownLocation()将经常返回null,尤其是在最近未使用位置提供程序(例如 GPS)的情况下。您仅getLastKnownLocation()在您并不真正需要位置(但希望拥有一个)或在getLastKnownLocation()返回时将使用其他技术null(例如,请求位置更新)的情况下使用。

于 2012-09-02T17:50:18.093 回答