Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Behind the scenes on Android

Behind the scenes on Android

My presentation from the 2019 May 9th event of Android Budapest meetup.

What’s allowed in the background on newer versions of Android. Coming up to Android Q, more and more restrictions apply to Android applications regarding background execution, and it is far from trivial to navigate these waters. Running Services, starting Activities from the background, getting location data, handling Intents and more. The talk will cover best practices and real world use cases where it is necessary to run logic automatically, without the user explicitly starting it.

Links from the end:
The CommonsBlog - Random Musings on Q Beta 3
https://commonsware.com/blog/2019/05/07/random-musings-q-beta-3.html

Android Q - Behavior changes
https://developer.android.com/preview/behavior-changes-all

Exploring Android Q: Bubbles
https://medium.com/google-developer-experts/exploring-android-q-bubbles-952d1c3be850

Balázs Gerlei

May 09, 2019
Tweet

More Decks by Balázs Gerlei

Other Decks in Technology

Transcript

  1. © 2019 Cognex 1 Behind the scenes on Android Balázs

    Gerlei | 2019.05.09. Dark Mode What’s still allowed to run in the background
  2. © 2019 Cognex 2 Behind the scenes on Android Balázs

    Gerlei | 2019.05.09. Dark Mode What’s still allowed to run in the background
  3. © 2019 Cognex 4 Android Q coming up fast §

    Beta 1 (initial release, beta) § Beta 2 (incremental update, beta) § Beta 3 (incremental update, beta) § Beta 4 (final APIs and official SDK, Play publishing, beta) § Beta 5 (release candidate for testing) § Beta 6 (release candidate for final testing) § Final release to AOSP and ecosystem
  4. © 2019 Cognex 5 § For performance and battery life

    more and more restrictions § Background execution gets limited § Implementing features, and maintaining existing ones is harder every day § Some changes only apply to apps targeting a certain API, but Play Store requires certain minimum API More restrictions are coming
  5. © 2019 Cognex 6 § Cannot run a Service in

    the background § When an app goes to background, the service stops § You have to start the Service with startForegroundService instead of startService § You have to call startForeground within ANR time § You have to show a Notification Starting Services Android 8 (API 26) Android 9 (API 28)
  6. © 2019 Cognex 7 § Apps no longer receive CONNECTIVITY_ACTION

    in BroadcastReceivers registered in the Manifest (if they target API 24 or higher) § Apps no longer receive ACTION_NEW_PICTURE and ACTION_NEW_VIDEO (regardless of target API) § Apps cannot register BroadcastReceivers in their manifest receiving implicit Broadcasts § There are some exceptions (like ACTION_BOOT_COMPLETED) § developer.android.com/guide/components/broadcast-exceptions Receiving Broadcasts Android 7 (API 24) Android 8 (API 26)
  7. © 2019 Cognex 8 § Not allowed to start an

    Activity from the background § This behavior enabled in Beta 3 by default § Can be toggled from Developer Options / Apps / Allow background activity start § Should use FullScreenIntent instead § Need USE_FULLSCREEN_INTENT (normal) permission § Changes to starting an Activity without a UI § android:theme="@android:style/Theme.NoDisplay” § Have to call finish() in onCreate() § commonsware.com/blog/2015/11/02/psa-android-6p0- theme.nodisplay-regression.html Starting Activites from the background Android 10 (will be API 29) Android 6 (API 23)
  8. © 2019 Cognex 9 Creating a FullScreenIntent val fullScreenIntent =

    Intent(this, CallActivity::class.java) val fullScreenPendingIntent = PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT) val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Incoming call") .setContentText("(919) 555-1234") .setPriority(NotificationCompat.PRIORITY_HIGH) .setCategory(NotificationCompat.CATEGORY_CALL) // Use a full-screen intent only for the highest-priority alerts where you // have an associated activity that you would like to launch after the user // interacts with the notification. Also, if your app targets Android Q, you // need to request the USE_FULL_SCREEN_INTENT permission in order for the // platform to invoke this notification. .setFullScreenIntent(fullScreenPendingIntent, true)
  9. © 2019 Cognex 10 Creating a FullScreenIntent val fullScreenIntent =

    Intent(this, CallActivity::class.java) val fullScreenPendingIntent = PendingIntent.getActivity(this, 0, fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT) val notificationBuilder = NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("Incoming call") .setContentText("(919) 555-1234") .setPriority(NotificationCompat.PRIORITY_HIGH) .setCategory(NotificationCompat.CATEGORY_CALL) // Use a full-screen intent only for the highest-priority alerts where you // have an associated activity that you would like to launch after the user // interacts with the notification. Also, if your app targets Android Q, you // need to request the USE_FULL_SCREEN_INTENT permission in order for the // platform to invoke this notification. .setFullScreenIntent(fullScreenPendingIntent, true)
  10. © 2019 Cognex 11 § Doze mode § Sleep when

    idle § Sleep when pocketed § Manufacturers keep adding their own solutions on top of it § Sharing Shortcuts § Sensor access § Background apps receive location updates less frequently § Connectivity APIs (like WiFi and Bluetooth require Location permission) Other things affecting background execution Android 8 (API 26) dontkillmyapp.com Android 10 (will be API 29) Android 7 (API 24) Android 6 (API 23)
  11. © 2019 Cognex 12 § Test thoroughly § Defer work

    until app start if possible § Use scheduled jobs instead of Services § If you have to use Services, use foreground Services § Avoid launching Activites from the background § Register Receivers at runtime § Think twice before building a feature or app relying on background execution Best Practices
  12. © 2019 Cognex 13 § SYSTEM_ALERT_WINDOW § Screen Overlays were

    always problematic § Facebook Chatheads, Status bar customization apps § Paid apps exists to fix the problem § DEPRECATION is imminent § They no longer work on Android Go devices § New Bubbles API will standardize floating interaction Screen Overlay and the Bubbles API What kind of scientist put bubbles in lemonade? Joke A FIZZicist! Android 10 (will be API 29)
  13. © 2019 Cognex 14 § Works on top of the

    Notification API § You create a notification as normal § Call Notification.BubbleMetadata.Builder to create a BubbleMetadata object § Use setBubbleMetadata to add the metadata to the notification § Best Practices § Only send a bubble if it is important enough such as ongoing communications, or if the user has explicitly requested a bubble § Bubbles can be disabled by the user. In that case, a bubble notification is shown as a normal notification § Processes that are launched from a bubble (such as activities and dialogs) appear within the bubble container. This means a bubble can have a task stack (and navigation) The Bubbles API
  14. © 2019 Cognex 15 The Bubbles API // Create bubble

    intent val target = Intent(context, BubbleActivity::class.java) val bubbleIntent = PendingIntent.getActivity(context, 0, target, 0 /* flags */) // Create bubble metadata val bubbleData = Notification.BubbleMetadata.Builder() .setDesiredHeight(600) // Note: although you can set the icon is not displayed in Q Beta 2 .setIcon(Icon.createWithResource(context, R.drawable.ic_message)) .setIntent(bubbleIntent) .build() // Create notification val chatBot = Person.Builder() .setBot(true) .setName("BubbleBot") .setImportant(true) .build()
  15. © 2019 Cognex 16 The Bubbles API // Create bubble

    intent val target = Intent(context, BubbleActivity::class.java) val bubbleIntent = PendingIntent.getActivity(context, 0, target, 0 /* flags */) // Create bubble metadata val bubbleData = Notification.BubbleMetadata.Builder() .setDesiredHeight(600) // Note: although you can set the icon is not displayed in Q Beta 2 .setIcon(Icon.createWithResource(context, R.drawable.ic_message)) .setIntent(bubbleIntent) .build() // Create notification val chatBot = Person.Builder() .setBot(true) .setName("BubbleBot") .setImportant(true) .build()
  16. © 2019 Cognex 17 The Bubbles API // Create bubble

    intent val target = Intent(context, BubbleActivity::class.java) val bubbleIntent = PendingIntent.getActivity(context, 0, target, 0 /* flags */) // Create bubble metadata val bubbleData = Notification.BubbleMetadata.Builder() .setDesiredHeight(600) // Note: although you can set the icon is not displayed in Q Beta 2 .setIcon(Icon.createWithResource(context, R.drawable.ic_message)) .setIntent(bubbleIntent) .build() // Create notification val chatBot = Person.Builder() .setBot(true) .setName("BubbleBot") .setImportant(true) .build()
  17. © 2019 Cognex 18 Thanks for listening! balazsgerlei.com @ github.com/

    balazsgerlei balazsgerlei § The CommonsBlog - Random Musings on Q Beta 3 § commonsware.com/blog/2019/05/07/random-musings-q-beta-3.html § Android Q - Behavior changes § developer.android.com/preview/behavior-changes-all § Exploring Android Q: Bubbles § medium.com/google-developer-experts/exploring-android-q-bubbles-952d1c3be850 We are hiring Engineers! jobs.cognex.com