2012-05-09 39 views
23

Có cách nào để truy cập GPS một lần thay vì có một looper liên tục kiểm tra cập nhật vị trí không?GPS Android - chỉ định vị một lần

Trong trường hợp của tôi, tất cả những gì tôi quan tâm là tìm các tọa độ hiện tại và không phải là kết nối liên tục với vệ tinh GPS. Có ai có bất kỳ ý tưởng làm thế nào điều này có thể được thực hiện? Cảm ơn trước.

Trả lời

31

Trước tiên hãy kiểm tra xem vị trí biết cuối cùng có gần đây không. Nếu không, tôi tin rằng bạn phải thiết lập trình lắng nghe onLocationChanged, nhưng một khi bạn nhận được vị trí hợp lệ đầu tiên của mình, bạn luôn có thể dừng luồng cập nhật.

Addition

public class Example extends Activity implements LocationListener { 
    LocationManager mLocationManager; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

     Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) { 
      // Do something with the recent location fix 
      // otherwise wait for the update below 
     } 
     else { 
      mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
     } 
    } 

    public void onLocationChanged(Location location) { 
     if (location != null) { 
      Log.v("Location Changed", location.getLatitude() + " and " + location.getLongitude()); 
      mLocationManager.removeUpdates(this); 
     } 
    } 

    // Required functions  
    public void onProviderDisabled(String arg0) {} 
    public void onProviderEnabled(String arg0) {} 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {} 
} 
+0

ngươi thats chính xác câu hỏi của tôi, tôi thiết lập một onLocationChanged và tôi muốn chỉ nhận được một vị trí hợp lệ .. làm thế nào tôi có thể dừng luồng cập nhật? Có lệnh nào không? Cảm ơn – progdoc

+0

Câu trả lời ngắn LocationManager.removeUpdates(). Câu trả lời dài, xem cập nhật của tôi. – Sam

+0

cảm ơn nó đã làm việc – progdoc

7

Dont sử dụng getLastKnownLocation vì đó có thể là trở về dữ liệu null hoặc cũ.

Mã này Chỉ tìm nạp vị trí khi nhấn một nút và không phải mọi lần. Số người sử dụng rời khỏi vị trí người nghe lắng nghe trong mọi trường hợp và giết chết tuổi thọ pin nên Sử dụng đoạn mã tôi đã gửi bằng cách thực hiện rất nhiều nghiên cứu:

// get the text view and buttons from the xml layout 
Button button = (Button) findViewById(R.id.btnGetLocation); 
final TextView latitude = (TextView) findViewById(R.id.textview4); 
final TextView longitude = (TextView) findViewById(R.id.textview5); 
final LocationListener locationListener = new LocationListener() { 
     @Override 
     public void onLocationChanged(Location location) { 
      mlocation = location; 
      Log.d("Location Changes", location.toString()); 
      latitude.setText(String.valueOf(location.getLatitude())); 
      longitude.setText(String.valueOf(location.getLongitude())); 
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      Log.d("Status Changed", String.valueOf(status)); 
     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      Log.d("Provider Enabled", provider); 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      Log.d("Provider Disabled", provider); 
     } 
    }; 

    // Now first make a criteria with your requirements 
    // this is done to save the battery life of the device 
    // there are various other other criteria you can search for.. 
    Criteria criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
    criteria.setPowerRequirement(Criteria.POWER_LOW); 
    criteria.setAltitudeRequired(false); 
    criteria.setBearingRequired(false); 
    criteria.setSpeedRequired(false); 
    criteria.setCostAllowed(true); 
    criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH); 
    criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH); 

    // Now create a location manager 
    final LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

    // This is the Best And IMPORTANT part 
    final Looper looper = null; 

    // Now whenever the button is clicked fetch the location one time 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      locationManager.requestSingleUpdate(criteria, locationListener, looper); 
     } 
    }); 
+0

Đây phải là câu trả lời được chấp nhận. Làm tốt lắm. – seekingStillness

Các vấn đề liên quan