#7 Floating Windows on Android: Boot Receiver
Tags: android, kotlin, tutorial, learning
Published: 2020-10-27 12:32:20

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](https://floatingapps.net); 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](https://localazy.com/blog/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:

```xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
```

## The Receiver

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

```kotlin
class BootReceiver : BroadcastReceiver() {  
  
  override fun onReceive(context: Context, intent: Intent?) {  
    context.startFloatingService()  
  }  
  
}
```

## Receiver Registration

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

```xml
<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>
```

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](https://github.com/vaclavhodek/quicknote_7).

## Stay Tuned

Eager to learn more about Android development? Follow me (<a href="https://twitter.com/vaclavhodek">@vaclavhodek</a>) and Localazy (<a href="https://twitter.com/localazy">@localazy</a>) on Twitter, or like <a href="https://www.facebook.com/localazy">Localazy on Facebook</a>.

## The Series

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

- [Floating Windows on Android 1: Jetpack Compose & Room](https://localazy.com/blog/floating-windows-on-android-1-jetpack-compose-and-room)
- [Floating Windows on Android 2: Foreground Service](https://localazy.com/blog/floating-windows-on-android-2-foreground-service)
- [Floating Windows on Android 3: Permissions](https://localazy.com/blog/floating-windows-on-android-3-permissions)
- [Floating Windows on Android 4: Floating Window](https://localazy.com/blog/floating-windows-on-android-4-floating-window)
- [Floating Windows on Android 5: Moving Window](https://localazy.com/blog/floating-windows-on-android-5-moving-window)
- [Floating Windows on Android 6: Keyboard Input](https://localazy.com/blog/floating-windows-on-android-6-keyboard-input)
- [Floating Windows on Android 7: Boot Receiver](https://localazy.com/blog/floating-windows-on-android-7-boot-receiver)
- [Floating Windows on Android 8: The Final App](https://localazy.com/blog/floating-windows-on-android-8-the-final-app)
- [Floating Windows on Android 9: Shortcomings](https://localazy.com/blog/floating-windows-on-android-9-shortcomings)
- [Floating Windows on Android 10: Tips & Tricks](https://localazy.com/blog/floating-windows-on-android-10-tips-and-tricks)