DEV Community

Cover image for #7 Floating Windows on Android: Boot Receiver
vaclavhodek for Localazy

Posted on

#7 Floating Windows on Android: Boot Receiver

Have you ever wondered how to make those floating windows used by Facebook Heads and other apps? Have you ever wanted to use the same technology in your app? It’s easy, and I will guide you through the whole process.

I'm the author of Floating Apps; the first app of its kind on Google Play and the most popular one with over 8 million downloads. After 6 years of the development of the app, I know a bit about it. It’s sometimes tricky, and I spent months reading documentation and Android source code and experimenting. I received feedback from tens of thousands of users and see various issues on different phones with different Android versions.

Here's what I learned along the way.

Before reading this article, it's recommended to go through Floating Windows on Android 6: Keyboard Input.

In this article, I will teach you how to start our service reliably when the device boots up.

Always Ready

Our floating service is launched from the main app. When the user turns off the device and later turns it on, she cannot make notes with our app until she manually starts it. That’s not comfortable.

However, we can quickly solve this by launching our service when the device boots up.

The Permission

We need to add permission to AndroidManifest.xml to listen for boot events:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Enter fullscreen mode Exit fullscreen mode

The Receiver

When the broadcast is received, just start the service. Nothing else is necessary to do.

class BootReceiver : BroadcastReceiver() {  

  override fun onReceive(context: Context, intent: Intent?) {  
    context.startFloatingService()  
  }  

}
Enter fullscreen mode Exit fullscreen mode

Receiver Registration

The last thing to do is to register our receiver in AndroidManifest.xml, so it receives required broadcast events.

<receiver android:name=".BootReceiver">  
  <intent-filter> 
    <action android:name="android.intent.action.BOOT_COMPLETED" />  
    <action android:name="android.intent.action.USER_PRESENT" />  
    <action android:name="android.intent.action.QUICKBOOT_POWERON" />  
  </intent-filter>
</receiver>
Enter fullscreen mode Exit fullscreen mode

You can notice that we listen to more events:

  • BOOT_COMPLETED - This is broadcast once after the device has finished booting.
  • USER_PRESENT - Sent when the user is present after the device wakes up (e.g. when the keyguard is gone).
  • QUICKBOOT_POWERON - This is broadcast that you don't find in the official Android documentation. It's sent on some devices and emulators when the device is restarted only.

With these three events, our service should be reliably launched when the device boots up.

Note: Please remember that when the app was force killed or stopped, it may not be started automatically on boot before the user launches it manually at least once again.

Settings

It’s out of the scope of this article, but for the app's production version, it would be useful to implement an option to disable the notification and disable starting the service when the device boots up.

Results

  1. Install the app and start the main app. The note notification appears.
  2. Real device: Turn the device off and on, or restart it.
  3. Emulator: Close the emulator and start it again from Android Virtual Device Manager. Don't start it by running the app!
  4. The notification should appear a while after the device boots up.

Source Code

The whole source code for this article is available on Github.

Stay Tuned

Eager to learn more about Android development? Follow me (@vaclavhodek) and Localazy (@localazy) on Twitter, or like Localazy on Facebook.

The Series

This article is part of the Floating Windows on Android series.

Top comments (0)