2012-09-02 136 views
0

我有这个问题我没有看到代码中的任何错误,但getLastKnownLocation每次都返回null。有任何想法吗 ?Android lastKnownLocation返回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"); 

    } 
} 

感谢

回答

3

getLastKnownLocation()经常会返回null,特别是在位置提供者(如GPS)最近没有使用。只有在您真的不需要需要位置(但希望拥有一个)或您将在其中使用其他技术(如果getLastKnownLocation()返回null(例如请求位置更新))的情况下使用getLastKnownLocation()

+0

我该如何找到位置?我可以强制操作系统使用GPS或网络或Wifi来查找当前位置 – 2013-10-30 12:23:45

+0

我如何programmaticaly打开谷歌位置服务我试过这个http://stackoverflow.com/questions/4721449/enable-gps-programatically- like-tasker/5305835#5305835但它不工作我需要它在2.3.3 gingebread – 2013-10-30 12:29:22

+0

@ user2726456:使用requestLocationUpdates()来请求位置更新。 – CommonsWare