Meaning of Criteria.POWER_LOW for LocationManager - android

The Criteria class is not documented in much detail.
Can someone please explain what POWER_HIGH and POWER_LOW change exactly?
Is it about battery drainage?
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
...
locationManager.requestSingleUpdate(criteria, locationListener, null);
Update:
I went through some of the source code. It turns out this parameter is ignored in my example because an accuracy is set as well.
createFromDeprecatedCriteria() in LocationRequest.java:
...
int quality;
switch (criteria.getAccuracy()) {
case Criteria.ACCURACY_COARSE:
quality = ACCURACY_BLOCK;
break;
case Criteria.ACCURACY_FINE:
quality = ACCURACY_FINE;
break;
default: {
if (criteria.getPowerRequirement() == Criteria.POWER_HIGH) {
quality = POWER_HIGH;
} else {
quality = POWER_LOW;
}
}
}
...

There are a number of different LocationProviders (gps, network, passive etc). The Criteria class can be used by the LocationManger to automatically select the best provider based on your requirements.
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setSpeedRequired(true);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
return locationManager.getBestProvider(criteria, true);
How the Criteria class is actually used can be seen here here.
Setting the power requirement to Criteria.POWER_LOW would probably just rule out the device using the GPS location provider (since it uses far more battery than the other passive types).

Related

requestLocationUpdates with Criteria doesn't work

Here's the code:
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setSpeedRequired(false);
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
manager.requestLocationUpdates(0,0, criteria, locationListener, null); // doesn't work
The Listener never gets an update if i pass criteria as argument. However, if i get provider from cirteria and pass to different arguments of request locationUpdates, it works
manager.requestLocationUpdates(manager.getBestProvider(criteria, true), 0, 0, locationListener); // works
Is the manager.requestLocationUpdates(0,0, criteria, listener, looper) method broken in the API?

android - how to get location update on every 10 meters?

in my application i have to update specific data on travelling of each 10 meters.
For that i m using LocationManager to get updates , but i am not getting correct answers.
I have used following code : Also added Permissions coarse, fine , internet .
locationManager.requestLocationUpdates(getProviderName(), 10000,
10, locationListener);
private String getProviderName() {
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.POWER_LOW);
// criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setSpeedRequired(true);
criteria.setAltitudeRequired(true);
criteria.setBearingRequired(true);
criteria.setCostAllowed(false);
criteria.setAccuracy(Criteria.ACCURACY_MEDIUM);
criteria.setSpeedAccuracy(Criteria.ACCURACY_MEDIUM);
criteria.setBearingAccuracy(Criteria.ACCURACY_MEDIUM);
return locationManager.getBestProvider(criteria, true);
}
Is this condition says LocationManager to update on every 10 seconds for 10 meters covered or not? am i right?

How to modify the criteria part and get location by GPS automatically in Android?

I would like to set that the device prefer to choose to get location by GPS rather than network provider, but the device still always using network provider to get location. How can I modify the criteria part and get location by GPS automatically?
The criteria I am using:
public void getLocationProvider()
{
try
{
Criteria mCriteria01 = new Criteria();
mCriteria01.setAccuracy(Criteria.ACCURACY_FINE);
mCriteria01.setAltitudeRequired(false);
mCriteria01.setBearingRequired(false);
mCriteria01.setCostAllowed(true);
mCriteria01.setPowerRequirement(Criteria.POWER_LOW);
strLocationProvider =
mLocationManager01.getBestProvider(mCriteria01, true);
mLocation01 = mLocationManager01.getLastKnownLocation
(strLocationProvider);
}
catch(Exception e)
{
mTextView01.setText(e.toString());
e.printStackTrace();
}
}
Instead of using the criteria to getBestProvider() and hope that it will be a GPS-based service, you can use the criteria to return all matching providers, iterate through them looking for a GPS-based compatible service, and use the one you find:
First, grab all matching providers for your criteria:
List<String> allMatchingProviders =
mLocationManager01.getProviders(mCriteria01, true);
Then iterate through the list and test each provider for satellite-based service:
foreach provider (allMatchingProviders)
{
...
if( provider->requiresSatellite() == true )
strLocationProvider = provider
...
}

java.lang.NullPointerException about gps api?

as the title i have a question about the gps api
follow is my code
LocationManager loctionManager;
String contextService=Context.LOCATION_SERVICE;
loctionManager=(LocationManager) getSystemService(contextService);
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 = loctionManager.getBestProvider(criteria, true);
Location location = loctionManager.getLastKnownLocation(provider);
double a=location.getLatitude();
double b=location.getLongitude();
Log.d(""+a,""+b);
then the error is java.lang.NullPointerException
anyone can help me?thx
getLastKnownLocation() can return null you need to ensure it does not.
In order to get a Location you need at first to set permission in your Manifest.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Then in your code you need to listen for location avalability. You do that by registering a LocationListener. The activity will display, and when Android will have a location for you it will call you back.
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
//Do what you want with your coordinates
}
loctionManager.requestLocationUpdates(LocationManager.GPS, 100, 1, locationListener);
If you are using the emulator and not a real device you need to force a location, you can use DDMS inside Eclipse to send a fake location:

criteria for both GPS and network

I would like to know how can I get location from the best provider
do I have to make two separate criteria 1 for the GPS and 1 for the network or is there a way to put them all together ?
this is my code when I add the COARSE criteria the GPS does not go on (no GPS flashing logo on the top of the screen) and when I use the FINE criteria I dont get any thing from the network.......so do I have to write criteria for both and switch between them for what ever is available or can they both be in the same criteria ?
because I have the "getBestProvider(criteria, true);" in my code so it should get the location from the best provider....right..??!
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
//// criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
String provider = locationManager.getBestProvider(criteria, true);
Using a getBestProvider with Criteria.ACCURACY_FINE will never return the NETWORK_PROVIDER, even though wifi position is usually quite accurate.
In my application, I use getProviders to get all providers matching my criteria, then add the NETWORK_PROVIDER if it is activated and not yet in the list.
Then I launch a requestLocationUpdates for each of these providers, making sure to call removeUpdates when I get a location accurate enough.
This way, if the location provided by the network is accurate enough, the gps provider will be turned off
#nicopico getBestProvider with Criteria.ACCURACY_FINE can return the NETWORK_PROVIDER if gps is not enabled
i use the following code in my app
public void getLocation() {
// Getting Google Play availability status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
// boolean network_enabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
// Showing status
if (status != ConnectionResult.SUCCESS) { // Google Play Services are not available
Log.e(TAG, "getLocation fired Google Play Services are not available");
mHandler.post(new UiToastCommunicaton(context.getApplicationContext(),
context.getResources().getString(R.string.gpserv_notfound)));
}
// Getting LocationManager object from System Service LOCATION_SERVICE
locationManager = (LocationManager) context.getSystemService(IntentService.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {//check if network location provider context on
Log.e(TAG, "network location provider not enabled");
} else {
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
//criteria.setPowerRequirement(Criteria.POWER_LOW);
// Getting the name of the best provider
provider = locationManager.getBestProvider(criteria, true);
// Check provider exists then request for location
if (provider != null) {
requestLocation(locationManager, provider);
} else {
//start wifi, gps, or tell user to do so
}
}
}
public void requestLocation(LocationManager locationManager, String provider) {
Log.e(TAG, "requestLocation fired getting location now");
if (provider != null) {
locationManager.requestLocationUpdates(provider, 0, 0, this);
//locationManager.requestLocationUpdates(provider, 0, 0, this, Looper.getMainLooper());
} else {
//tell user what has happened
}
}
this code is in a class that implements locationListener
you can edit this line
locationManager.requestLocationUpdates(provider, 0, 0, this);
to reflect your implementation of locationListener

Categories

Resources