Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

17
Get truly connected Will it run or will it not run? Background processes Anna Lifshits Agmon

Transcript of Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

Page 1: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

Get truly connected

Will it run or will it not run?Background processes

Anna Lifshits Agmon

Page 2: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-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.

Page 3: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

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.

Page 4: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

The team

Itai LeshemCEO

Anna Lifshits-Agmon

CTO

Denis SurkesSenior

Architect

Dor AlonAndroid

Developer

Page 5: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

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

Page 6: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

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!

Page 7: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

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

Page 8: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

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.

Page 9: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

Location Tracking Service

Tested and worked fine on with 100 test user.

And then ……

Doze and App Standby modes

Page 10: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

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.

Page 11: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

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.

Page 12: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

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.

Page 13: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

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

Page 14: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

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

Page 15: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

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 ).

Page 16: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

Next challenges

Page 17: Will it run or will it not run? Background processes in Android 6 - Anna Lifshitz-Agmon

Thanks for listening!

[email protected]