Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gps listener/receiver wakes up CPU

Tags:

android

gps

From what I can see in the AOSP code, a wakelock is grabbed during onLocationChanged or when Android fires the intent to call the registered location receiver. So, you don't need to take a wakelock to do the processing, or at least you should take a wakelock for async operation done in onLocationChanged or if you start an intentService from the receiver and so on. Now my question: does the gps wake up the cpu? I mean: when the gps trigger is received by the OS the wakelock grants that the information can be processed, but before? Will the gps generate an interrupt to wake up the cpu? Even if you use the receiver version, it doesn't grant you that the gps wakes up the cpu, or is the listener/pendignIntent like the alarm of alarmManager and we can trust on it? Is it needed to grab a wakelock to keep the system on to receive the information?

like image 679
greywolf82 Avatar asked Sep 07 '14 20:09

greywolf82


2 Answers

I think maybe you are thinking about it a little backwards. Generally the app or the user will trigger the GPS, not the GPS triggering the wake lock.

Basically, if you ask for updates, you should get them (depending on a few things).

Criteria
Whether the app keeps the GPS awake or not depends greatly on the Criteria

According to the LocationManager.requestLocationUpdates(long minTime, float minDistance, Criteria c, PendingIntent i) method documentation:

The location update interval can be controlled using the minTime parameter. The elapsed time between location updates will never be less than minTime, although it can be more depending on the Location Provider implementation and the update interval requested by other applications.

Active Providers

GPS and the Network Provider are considered "Active Providers". Both take time to start up and get a new fix and consume battery.

The system will attempt to wake/use an active provider to meet your Criteria, and if you are using a BroadcastReceiver, then the onRecieve method is guaranteed to complete.

If you ask for the GPS every hour, the system should be waking the device, and running the GPS and checking it against your Criteria every hour assuming the provider is enabled.

In case the provider is disabled by the user, updates will stop, and a provider availability update will be sent. As soon as the provider is enabled again, location updates will immediately resume and a provider availability update sent.

Additional Concerns & Triggers

This is why they recommend considering additional triggers for listening like when the provider is enabled or disabled. These can be combined with what @danny177 mentioned such as the charging status and battery level. Even the current Network State can matter here too.

Providers can also send status updates, at any time, with extra's specific to the provider. If a callback was supplied then status and availability updates are via onProviderDisabled(String), onProviderEnabled(String) or onStatusChanged(String, int, Bundle). Alternately, if a pending intent was supplied then status and availability updates are broadcast intents with extra keys of KEY_PROVIDER_ENABLED or KEY_STATUS_CHANGED.

Finally you could also use the passive provider. This will only give you an update if another apps wakes one of the active providers for you.

If your application wants to passively observe location updates triggered by other applications, but not consume any additional power otherwise, then use the PASSIVE_PROVIDER This provider does not actively turn on or modify active location providers

Location Strategies

Generally, you want to go from least accurate to most, lowest power to highest, etc.. For example, you could use the PASSIVE_PROVIDER to see if the user has move more than 100 meters. Then if they have, request active location updates from the network provider to see if they are close to a specific location, then if that is true, use the GPS for something even more specific.

See: Location Strategies

Determining which to use and trust is a matter of trade-offs in accuracy, speed, and battery-efficiency.

Google Play Services

Finally, you cal also use Google Play Services as a location provider and piggyback on it's location updates. This also gives you "free" geofencing and geocoding.

like image 160
pjco Avatar answered Oct 16 '22 12:10

pjco


You don't need a wakelock to process locations received in onLocationChanged().

However background gps logging over a long time period might warrant the use of a wakelock

Before turning on a wakelock consider the battery level, if the device is charging, when the wakelock will be released.

Universal Assumptions for background wakelock.

  • Device is charging then wakelock.
  • Device not charging and above N% then wakelock else release wakelock
  • processing is done release wakelock.

EDIT

Please use a BroadcastReciver with your wake lock.

final IntentFilter theFilter = new IntentFilter();
        theFilter.addAction(Intent.ACTION_BATTERY_LOW);

        registerReceiver(yourReceiver, theFilter);




private static BroadcastReceiver yourReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            String s = intent.getAction();
            if (s != null) {
                if (s.equals(LocationManager.PROVIDERS_BATTERY_LOW)) {
                    cancelMyWakeLock();
                }               }
        }
    }
};

Remember to un-register in onStop() unregister code not shown.

like image 1
danny117 Avatar answered Oct 16 '22 14:10

danny117