Search in sources :

Example 16 with Criteria

use of android.location.Criteria in project enroscar by stanfy.

the class LocationBinder method startListening.

/**
   * Please call it from the main thread (with looper).
   * @param context context instance
   */
public void startListening(final Context context) {
    if (DEBUG) {
        Log.d(TAG, "Start location listening...");
    }
    try {
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (locationManager == null) {
            return;
        }
    } catch (final Exception e) {
        return;
    }
    myHandler = myLooper != null ? new MyHandler(myLooper) : new MyHandler();
    if (listener != null) {
        listener.onLocationStart();
    }
    final Location last = getLastKnown(locationManager);
    if (last != null) {
        newLocation(last);
    }
    final Criteria c = new Criteria();
    c.setAltitudeRequired(false);
    c.setSpeedRequired(false);
    c.setBearingRequired(false);
    c.setCostAllowed(false);
    c.setPowerRequirement(Criteria.POWER_LOW);
    c.setAccuracy(Criteria.ACCURACY_COARSE);
    final String coarseProvider = locationManager.getBestProvider(c, false);
    c.setPowerRequirement(Criteria.NO_REQUIREMENT);
    c.setAccuracy(Criteria.ACCURACY_FINE);
    final String fineProvider = locationManager.getBestProvider(c, false);
    if (DEBUG) {
        Log.d(TAG, "Providers " + coarseProvider + "/" + fineProvider);
    }
    final long minTime = 60000;
    final int minDistance = 50;
    if (coarseProvider != null) {
        if (DEBUG) {
            Log.d(TAG, "Register for " + coarseProvider);
        }
        locationManager.requestLocationUpdates(coarseProvider, minTime, minDistance, coarseListener);
        myHandler.sendEmptyMessageDelayed(MSG_STOP_COARSE_PROVIDER, MAX_COARSE_PROVIDER_LISTEN_TIME);
    }
    if (fineProvider != null) {
        if (DEBUG) {
            Log.d(TAG, "Register for " + fineProvider);
        }
        locationManager.requestLocationUpdates(fineProvider, minTime, minDistance, fineListener);
        myHandler.sendEmptyMessageDelayed(MSG_STOP_FINE_PROVIDER, MAX_FINE_PROVIDER_LISTEN_TIME);
    }
}
Also used : Criteria(android.location.Criteria) SuppressLint(android.annotation.SuppressLint) Location(android.location.Location)

Example 17 with Criteria

use of android.location.Criteria in project AndroidSDK-RecipeBook by gabu.

the class Recipe073 method onResume.

@Override
protected void onResume() {
    super.onResume();
    // 現在の状況に最適なプロバイダを取得します。
    // Criteriaで細かい条件が指定できますが今回はデフォルトで
    String provider;
    provider = mManager.getBestProvider(new Criteria(), true);
    if (provider == null) {
    // 位置情報が取得できるプロバイダがありません。
    // Wifiにも3Gにも繋がっていないなど。
    }
    mManager.requestLocationUpdates(provider, 0, 0, this);
    // 最後に取得した位置情報を取得
    Location location = mManager.getLastKnownLocation(provider);
    if (location != null)
        onLocationChanged(location);
}
Also used : Criteria(android.location.Criteria) Location(android.location.Location)

Example 18 with Criteria

use of android.location.Criteria in project gdk-compass-sample by googleglass.

the class OrientationManager method start.

/**
     * Starts tracking the user's location and orientation. After calling this method, any
     * {@link OnChangedListener}s added to this object will be notified of these events.
     */
public void start() {
    if (!mTracking) {
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR), SensorManager.SENSOR_DELAY_UI);
        // The rotation vector sensor doesn't give us accuracy updates, so we observe the
        // magnetic field sensor solely for those.
        mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_UI);
        Location lastLocation = mLocationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
        if (lastLocation != null) {
            long locationAge = lastLocation.getTime() - System.currentTimeMillis();
            if (locationAge < MAX_LOCATION_AGE_MILLIS) {
                mLocation = lastLocation;
                updateGeomagneticField();
            }
        }
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setBearingRequired(false);
        criteria.setSpeedRequired(false);
        List<String> providers = mLocationManager.getProviders(criteria, true);
        for (String provider : providers) {
            mLocationManager.requestLocationUpdates(provider, MILLIS_BETWEEN_LOCATIONS, METERS_BETWEEN_LOCATIONS, mLocationListener, Looper.getMainLooper());
        }
        mTracking = true;
    }
}
Also used : Criteria(android.location.Criteria) Location(android.location.Location)

Example 19 with Criteria

use of android.location.Criteria in project android_frameworks_base by DirtyUnicorns.

the class LocationManagerTest method testGetBestProviderEmptyCriteria.

public void testGetBestProviderEmptyCriteria() {
    String p = manager.getBestProvider(new Criteria(), true);
    assertNotNull(p);
}
Also used : Criteria(android.location.Criteria)

Example 20 with Criteria

use of android.location.Criteria in project android_frameworks_base by AOSPA.

the class FusedPrintersProvider method onStartLoading.

@Override
protected void onStartLoading() {
    if (DEBUG) {
        Log.i(LOG_TAG, "onStartLoading() " + FusedPrintersProvider.this.hashCode());
    }
    mLocationManager.requestLocationUpdates(LocationRequest.create().setQuality(LocationRequest.POWER_LOW).setInterval(LOCATION_UPDATE_MS), this, Looper.getMainLooper());
    Location lastLocation = mLocationManager.getLastLocation();
    if (lastLocation != null) {
        onLocationChanged(lastLocation);
    }
    // Jumpstart location with a single forced update
    Criteria oneTimeCriteria = new Criteria();
    oneTimeCriteria.setAccuracy(Criteria.ACCURACY_FINE);
    mLocationManager.requestSingleUpdate(oneTimeCriteria, this, Looper.getMainLooper());
    // The contract is that if we already have a valid,
    // result the we have to deliver it immediately.
    (new Handler(Looper.getMainLooper())).post(new Runnable() {

        @Override
        public void run() {
            deliverResult(new ArrayList<>(mPrinters));
        }
    });
    // Always load the data to ensure discovery period is
    // started and to make sure obsolete printers are updated.
    onForceLoad();
}
Also used : ArrayList(java.util.ArrayList) Handler(android.os.Handler) Criteria(android.location.Criteria) Location(android.location.Location)

Aggregations

Criteria (android.location.Criteria)51 Location (android.location.Location)19 Test (org.junit.Test)12 LocationManager (android.location.LocationManager)8 ArrayList (java.util.ArrayList)8 PendingIntent (android.app.PendingIntent)6 Intent (android.content.Intent)6 Bundle (android.os.Bundle)6 Handler (android.os.Handler)6 LocationListener (android.location.LocationListener)5 SuppressLint (android.annotation.SuppressLint)2 FacebookException (com.facebook.FacebookException)2 HashMap (java.util.HashMap)2 Prefs (nodomain.freeyourgadget.gadgetbridge.util.Prefs)2 PackageManager (android.content.pm.PackageManager)1 ResolveInfo (android.content.pm.ResolveInfo)1 EditTextPreference (android.preference.EditTextPreference)1 ListPreference (android.preference.ListPreference)1 Preference (android.preference.Preference)1 PreferenceCategory (android.preference.PreferenceCategory)1