2012-05-09 17 views
23

स्थिति प्राप्त करें क्या एक लूपर होने के बजाय जीपीएस तक पहुंचने का कोई तरीका है जो लगातार स्थान अपडेट के लिए जांच करता है?जीपीएस एंड्रॉइड - केवल एक बार

मेरे परिदृश्य में मुझे दिलचस्पी है कि वर्तमान समन्वय ढूंढ रहा है और जीपीएस उपग्रह के साथ निरंतर कनेक्शन नहीं है। क्या किसी के पास कोई विचार है कि यह कैसे किया जा सकता है? अग्रिम में धन्यवाद।

उत्तर

31

पहले जांचें कि अंतिम पता स्थान हाल ही में है या नहीं। यदि नहीं, तो मुझे विश्वास है कि आपको स्थान परिवर्तन किए गए श्रोता पर सेट अप करना होगा, लेकिन एक बार जब आप अपना पहला मान्य स्थान प्राप्त कर लेंगे तो आप हमेशा अपडेट की स्ट्रीम को रोक सकते हैं।

अलावा

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

तु thats ठीक मेरे सवाल का, मैं एक की स्थापना की onLocationChanged और मैं सिर्फ एक वैध स्थान प्राप्त करना चाहता हूं..मैं अपडेट की स्ट्रीम कैसे रोक सकता हूं? क्या कोई आदेश है? धन्यवाद – progdoc

+0

संक्षिप्त उत्तर स्थानमैंजर.remove अद्यतन()। लंबा जवाब, मेरा अपडेट देखें। – Sam

+0

धन्यवाद यह – progdoc

7

न getLastKnownLocation का उपयोग करें कि क्योंकि अशक्त या पुराने डेटा लौटने हो सकता है।

यह कोड बटन दबाए जाने के बाद केवल स्थान प्राप्त करता है और हर बार नहीं। लोग स्थान श्रोता प्रत्येक स्थिति में सुनने को छोड़ने के लिए इस्तेमाल करते हैं और कि बैटरी जीवन तो कोड स्निपेट मैं अनुसंधान के बहुत सारे करने से पोस्ट किया है का उपयोग करें मारता है:

// 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

यह स्वीकार्य उत्तर होना चाहिए। अच्छा कार्य। – seekingStillness

संबंधित मुद्दे