Sobes.tech
Junior — Middle

Which service stream is activated automatically when the system starts?

sobes.tech AI

Answer from AI

In Android, there is no Service thread that is automatically activated at system startup by default. However, if an application registers a BroadcastReceiver for the BOOT_COMPLETED event and starts a Service within it, then this Service can automatically start after the system boots.

Example: to have a service start after device boot, specify in the manifest:

<receiver android:name=".BootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<service android:name=".MyService" />

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

And in BootReceiver, start the service:

class BootReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
            val serviceIntent = Intent(context, MyService::class.java)
            context.startService(serviceIntent)
        }
    }
}

Thus, the service starts automatically after system boot, but only if it is specifically configured to do so.