Example usage for android.location Criteria setPowerRequirement

List of usage examples for android.location Criteria setPowerRequirement

Introduction

In this page you can find the example usage for android.location Criteria setPowerRequirement.

Prototype

public void setPowerRequirement(int level) 

Source Link

Document

Indicates the desired maximum power level.

Usage

From source file:com.example.mohamed.a3qaqer.RegisterActvity.java

private void getloc() {
    if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        loc_string = "none";// indicate that no gps data
        return;//from  w w  w . j a  va2 s . co  m
    }
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setBearingRequired(true);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    criteria.setAltitudeRequired(false);
    String bestProvider = locationManager.getBestProvider(criteria, true);
    if (ActivityCompat.checkSelfPermission(this,
            android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this,
                    android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    locationManager.requestLocationUpdates(bestProvider, 2000, 10, new LocationListener() {
        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {
        }

        @Override
        public void onProviderEnabled(String s) {
        }

        @Override
        public void onProviderDisabled(String s) {
        }

        @Override
        public void onLocationChanged(final Location location) {
        }
    });
    Location myLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
    double longitude = myLocation.getLongitude();
    double latitude = myLocation.getLatitude();
    loc_string = latitude + "-" + longitude;

}

From source file:com.careme.apvereda.careme.AccumulatorService.java

@Override
public int onStartCommand(Intent intenc, int flags, int idArranque) {
    SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("basicData",
            Context.MODE_PRIVATE);
    String email = sharedPref.getString("email", "");

    //Uncomment to use Nimbees features

    /*if (email.compareTo("") != 0) {
    try {/*from  w  ww  .j  av a 2 s.  c o  m*/
        // Initialize library calling the init method on the Nimbees Client
        NimbeesClient.init(this);
    } catch (NimbeesException e) {
        e.printStackTrace();
    }
    NimbeesClient.getUserManager().register("email", new NimbeesRegistrationCallback() {
        @Override
        public void onSuccess() {
            /* Registration was successful!
            Toast.makeText(getApplicationContext(),
                    "xito en el registro", Toast.LENGTH_LONG).show();
        }
            
        @Override
        public void onFailure(NimbeesException failure) {
            /* Registration failed
            Toast.makeText(getApplicationContext(),
                    "Fallo en el registro", Toast.LENGTH_LONG).show();
        }
    });
    }
    */
    //Toast.makeText(this,"Servicio arrancado "+ idArranque, Toast.LENGTH_SHORT).show();

    // Obtain a reference to the Location Manager
    locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria crit = new Criteria();
    crit.setAccuracy(Criteria.ACCURACY_FINE);
    crit.setPowerRequirement(Criteria.POWER_LOW);
    provider = locManager.getBestProvider(crit, true);

    // And register to obtain current location updates
    locListener = new LocationListener() {
        public void onLocationChanged(Location loc) {
            // Insert a new entry on History
            History history = new History(loc.getLatitude(), loc.getLongitude(), new Date());
            db.insertHistory(history);
            //Uncomment to use Nimbees features
            /*
            try {
            sendPersonalizado(loc);
            } catch (Exception e) {
            }*/
            /*
            Monitor makes the monitoring of the user to determine if he/she has lost
            */
            monitor(loc);
        }

        @Override
        public void onStatusChanged(String provider, int stat, Bundle extras) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
        }
    };
    // We want to update the current location every time the user moves MIN_DISTANCE
    locManager.requestLocationUpdates(provider, 0, MIN_DISTANCE, locListener);
    return START_STICKY;
}

From source file:com.findcab.driver.activity.Signup.java

/**
 * ?GPS?/* w  w w.j  a v  a  2 s.c  o m*/
 */
private void initLocation() {

    LocationManager locationManager;
    String serviceName = Context.LOCATION_SERVICE;
    locationManager = (LocationManager) this.getSystemService(serviceName);
    // ?
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);

    String provider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(provider);
    if (location != null) {

        lat = location.getLatitude();
        lng = location.getLongitude();

    }
}

From source file:org.navitproject.navit.NavitVehicle.java

/**
 * @brief Creates a new {@code NavitVehicle}
 *
 * @param context/*from w w w .ja  va 2s.c o  m*/
 * @param pcbid The address of the position callback function called when a location update is received
 * @param scbid The address of the status callback function called when a status update is received
 * @param fcbid The address of the fix callback function called when a
 * {@code android.location.GPS_FIX_CHANGE} is received, indicating a change in GPS fix status
 */
NavitVehicle(Context context, int pcbid, int scbid, int fcbid) {
    if (ContextCompat.checkSelfPermission(context,
            android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // Permission is not granted
        return;
    }
    this.context = context;
    sLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    preciseLocationListener = new NavitLocationListener();
    preciseLocationListener.precise = true;
    fastLocationListener = new NavitLocationListener();

    /* Use 2 LocationProviders, one precise (usually GPS), and one
       not so precise, but possible faster. The fast provider is
       disabled when the precise provider gets its first fix. */

    // Selection criteria for the precise provider
    Criteria highCriteria = new Criteria();
    highCriteria.setAccuracy(Criteria.ACCURACY_FINE);
    highCriteria.setAltitudeRequired(true);
    highCriteria.setBearingRequired(true);
    highCriteria.setCostAllowed(true);
    highCriteria.setPowerRequirement(Criteria.POWER_HIGH);

    // Selection criteria for the fast provider
    Criteria lowCriteria = new Criteria();
    lowCriteria.setAccuracy(Criteria.ACCURACY_COARSE);
    lowCriteria.setAltitudeRequired(false);
    lowCriteria.setBearingRequired(false);
    lowCriteria.setCostAllowed(true);
    lowCriteria.setPowerRequirement(Criteria.POWER_HIGH);

    Log.e("NavitVehicle", "Providers " + sLocationManager.getAllProviders());

    preciseProvider = sLocationManager.getBestProvider(highCriteria, false);
    Log.e("NavitVehicle", "Precise Provider " + preciseProvider);
    fastProvider = sLocationManager.getBestProvider(lowCriteria, false);
    Log.e("NavitVehicle", "Fast Provider " + fastProvider);
    vehicle_pcbid = pcbid;
    vehicle_scbid = scbid;
    vehicle_fcbid = fcbid;

    context.registerReceiver(preciseLocationListener, new IntentFilter(GPS_FIX_CHANGE));
    sLocationManager.requestLocationUpdates(preciseProvider, 0, 0, preciseLocationListener);
    sLocationManager.addGpsStatusListener(preciseLocationListener);

    /*
     * Since Android criteria have no way to specify "fast fix", lowCriteria may return the same
     * provider as highCriteria, even if others are available. In this case, do not register two
     * listeners for the same provider but pick the fast provider manually. (Usually there will
     * only be two providers in total, which makes the choice easy.)
     */
    if (fastProvider == null || preciseProvider.compareTo(fastProvider) == 0) {
        List<String> fastProviderList = sLocationManager.getProviders(lowCriteria, false);
        fastProvider = null;
        for (String fastCandidate : fastProviderList) {
            if (preciseProvider.compareTo(fastCandidate) != 0) {
                fastProvider = fastCandidate;
                break;
            }
        }
    }
    if (fastProvider != null) {
        sLocationManager.requestLocationUpdates(fastProvider, 0, 0, fastLocationListener);
    }
}

From source file:com.findcab.driver.activity.Signup.java

private void initView() {
    context = this;
    back = (Button) findViewById(R.id.back);
    back.setOnClickListener(this);
    start = (Button) findViewById(R.id.start);
    start.setOnClickListener(this);

    edit_name = (EditText) findViewById(R.id.name);
    edit_mobile = (EditText) findViewById(R.id.mobile);
    edit_password = (EditText) findViewById(R.id.password);

    checkBox = (CheckBox) findViewById(R.id.checkBox);

    item = (TextView) findViewById(R.id.item);
    item.setOnClickListener(this);

    if (initGPS()) {

        LocationManager locationManager;
        String serviceName = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) this.getSystemService(serviceName);
        // ?// w w  w. j a  v  a  2 s .co m
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(Criteria.POWER_LOW);

        String provider = locationManager.getBestProvider(criteria, true);
        location = locationManager.getLastKnownLocation(provider);
        if (location != null) {
            lat = location.getLatitude();
            lng = location.getLongitude();
        }
    }
}

From source file:org.wso2.edgeanalyticsservice.LocationSystemService.java

/** Initialize the location system Service and set the minimal update distance and time  */
public void startLocationService(Context context) {
    mContext = context;//from   ww w  .  j  ava  2 s.c o  m
    mLocationManager = (LocationManager) mContext.getSystemService(mContext.LOCATION_SERVICE);
    Criteria locationCritera = new Criteria();
    locationCritera.setAccuracy(Criteria.ACCURACY_COARSE);
    locationCritera.setAltitudeRequired(false);
    locationCritera.setBearingRequired(false);
    locationCritera.setCostAllowed(true);
    locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);

    String providerName = mLocationManager.getBestProvider(locationCritera, true);

    if (providerName != null && mLocationManager.isProviderEnabled(providerName)) {
        if (ActivityCompat.checkSelfPermission(mContext,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(mContext,
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public
            // void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            Log.e("Location", "No permission");
            return;
        }
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES,
                (float) MINIMUM_DISTANCE_FOR_UPDATES, new MyLocationListner(), Looper.getMainLooper());
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES,
                (float) MINIMUM_DISTANCE_FOR_UPDATES, new MyLocationListner(), Looper.getMainLooper());
        mlocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        //        mTaskManager.sendLocationData(new double[]{mlocation.getLatitude(), mlocation.getLongitude()});
        mLocationListner = new MyLocationListner();
        synchronized (this) {
            started = true;
        }
    } else {
        // Provider not enabled, prompt user to enable it
        Toast.makeText(mContext, "Please turn on GPS", Toast.LENGTH_LONG).show();
        Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(myIntent);
    }
}

From source file:ch.hesso.master.sweetcity.activity.map.MapActivity.java

public void showCurrentPosition() {
    if (map == null)
        return;//w  w w.j  a v a2s.c o  m

    // Enabling MyLocation Layer of Google Map
    map.setMyLocationEnabled(true);

    Criteria lightCriteria = new Criteria();
    lightCriteria.setAccuracy(Criteria.ACCURACY_COARSE);
    lightCriteria.setAltitudeRequired(false);
    lightCriteria.setBearingRequired(false);
    lightCriteria.setCostAllowed(false);
    lightCriteria.setHorizontalAccuracy(Criteria.ACCURACY_MEDIUM);
    lightCriteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
    lightCriteria.setSpeedAccuracy(Criteria.NO_REQUIREMENT);
    lightCriteria.setSpeedRequired(false);

    listener = new MapLocationListener(map);

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationManager.requestSingleUpdate(lightCriteria, listener, null);
    locationProvider = locationManager.getBestProvider(new Criteria(), true);
    Location location = locationManager.getLastKnownLocation(locationProvider);

    if (location != null) {
        listener.onLocationChanged(location);
        map.animateCamera(CameraUpdateFactory.zoomTo(15));
    }
}

From source file:ch.hesso.master.sweetcity.activity.report.ReportActivity.java

void updateCurrentLocation() {
    if (this.locationListener == null)
        this.locationListener = new LocationListener() {
            @Override//from ww  w  .ja va 2 s.c  o  m
            public void onLocationChanged(Location location) {
                ReportActivity.this.currentLocation = location;
            }

            @Override
            public void onProviderDisabled(String provider) {
            }

            @Override
            public void onProviderEnabled(String provider) {
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
        };

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(false);
    criteria.setHorizontalAccuracy(Criteria.ACCURACY_MEDIUM);
    criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
    criteria.setSpeedAccuracy(Criteria.NO_REQUIREMENT);
    criteria.setSpeedRequired(false);

    LocationManager locationManager = (LocationManager) ReportActivity.this.getSystemService(LOCATION_SERVICE);
    locationManager.requestSingleUpdate(criteria, this.locationListener, null);
}

From source file:com.quantcast.measurement.service.QCLocation.java

void setupLocManager(Context appContext) {
    if (appContext == null)
        return;//from  w  ww  .  j a  v a 2s .  c  o  m

    _locManager = (LocationManager) appContext.getSystemService(Context.LOCATION_SERVICE);
    if (_locManager != null) {
        //specifically set our Criteria. All we need is a general location
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(false);
        criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
        criteria.setSpeedRequired(false);

        _myProvider = _locManager.getBestProvider(criteria, true);

        _geocoder = new Geocoder(appContext);
    }
    QCLog.i(TAG, "Setting location provider " + _myProvider);
}

From source file:com.anton.gavel.GavelMain.java

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override//from w  w w. j  a v a  2  s .c  om
public void onClick(View arg0) {
    //click listener for location button

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.POWER_HIGH);
    //String provider = locationManager.getBestProvider(criteria, true);

    locationManager.requestSingleUpdate(criteria, new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD)
                createDialog(DIALOG_NO_GEOCODING);
            else if (Geocoder.isPresent())
                (new ReverseGeocodingTask(getBaseContext())).execute(new Location[] { location });
            // Invoking reverse geocoding in an AsyncTask. 
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

    }, null);

}