will it run or will it not run? background processes in android 6 - anna lifshitz-agmon
TRANSCRIPT
Get truly connected
Will it run or will it not run?Background processes
Anna Lifshits Agmon
About usAndroid mobile messaging app that opens up the way people find and connect with each other.Connect with people based on their location and interests while keeping your privacy.Return the power back to the users which provide the data.
ProductAndroid mobile up released on May 2016. Newest version released on September 2016.
50,000+ downloads
Was featured as Top Free and Social Top Trending in Israel.
Thousands of new and active users on a daily basis.
The team
Itai LeshemCEO
Anna Lifshits-Agmon
CTO
Denis SurkesSenior
Architect
Dor AlonAndroid
Developer
Location Tracking Service
Sample user location every x time and send the data to the server every y time.
Should run also when the user does not use the app.
Should run also after device reboot.
AlarmManager WakeLockLocationTrackerService
Keeps the CPU on while the process is running
Takes location samples, stores and sends them to the serverSchedules the service
Location Tracking Service
AlarmManager
final AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
final PendingIntent wakeupIntent = PendingIntent.getService(context, 0, new Intent(context, LocationTrackerService.class), PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + Consts.TRACKER_SAMPLE_EVERY_MS, Consts.TRACKER_SAMPLE_EVERY_MS, wakeupIntent);
Wakes up the device and fires the pending intent after the specified length of time has
elapsed since device boot
Synchronizes repeating alarms from multiple apps and fires them at the same time. Best practice for not draining battery
Handle with care not to cause battery drain!
Location Tracking Service
AlarmManager
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> …..<receiver android:name=".tracker.BootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter></receiver>
Automatically restart after a
reboot.
public class BootReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) { if (intent != null && intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) { Utils.startTrackerAlarm(context); } }}
AndroidManifest.xml
Location Tracking Service
WakeLock
Handle with care not to cause battery drain!
Acquired at the beginning of the process.
Make sure to acquire only once.
Released when not needed anymore.
Location Tracking Service
Tested and worked fine on with 100 test user.
And then ……
Doze and App Standby modes
What is Doze?
Doze restrictions:
1. Network access suspended.2. Ignore Wake Locks.3. The system does not perform Wi-Fi scans.4. The system does not allow Sync Adapters to
run.5. The system does not allow JobScheduler to
run.6. Need to adapt AlarmManager.
Doze Adaptations
AlarmManager
if (isMarshmallowOrHigher()) { alarmManager.setAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + Consts.TRACKER_SAMPLE_EVERY_MS, wakeupIntent);} else { alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + Consts.TRACKER_SAMPLE_EVERY_MS, Consts.TRACKER_SAMPLE_EVERY_MS, wakeupIntent);}
With this method the alarm fires also in Doze, but is not repeating. We need to fire it
again within the process.
What is App Standby?
The system determines that an app is idle when the user is not actively using it. The system makes this determination when the user does not touch the app for a certain period of time.
Standby disabled when the device is plugged into a power supply.If the device is idle for long periods of time, the system allows idle apps network access around once a day.Possible solution:
Add notificaiton to notification that users see on the lock screen or in the notification tray.
Other Adaptations
If using GCM – high priority messages are optimized to work with Doze and App Standby modes.
Whitelist for Doze and Standby mode exists that enables using netwoek and holding partial wake locks during Doze and App Standby.Not all apps can be added to the whitelist and the user should approve it.Test, Test and Test
https://developer.android.com/training/monitoring-device-state/doze-standby.htmlc
Server Side Adaptations
Tested and worked fine on with 1000 test user.
And then ……
We realized that with higher scale of users we want to have control from the server side as well
Server Side Adaptations
For each user keep when was the last time location data / keep alive was received.Run a scheduled process that sends a SILENT Notification with High Priority to those users from whom no data was received for X time.
The process awakes users from Doze/App Stanby mode and marks users who uninstalled the app ( as a bonus ).
Next challenges
Thanks for listening!