37

常に位置情報の更新をチェックするルーパーを使用する代わりに、一度だけ GPS にアクセスする方法はありますか?

私のシナリオでは、GPS 衛星との継続的な接続ではなく、現在の座標を見つけることに関心があります。これを行う方法を知っている人はいますか? 前もって感謝します。

4

2 に答える 2

42

null または古いデータが返される可能性があるため、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);
       }
   });
于 2016-08-05T16:54:23.033 に答える
40

最初に、最後にわかった場所が最近のものかどうかを確認します。そうでない場合は、onLocationChangedリスナーを設定する必要があると思いますが、最初の有効な場所を取得したら、いつでも更新のストリームを停止できます。

添加

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) {}
}
于 2012-05-09T21:35:45.160 に答える