admin管理员组

文章数量:1405329

I am trying to automatically launch my application when an Android TV restarts. I have already implemented the following approaches, but none seem to work on API 34 (Android 14+) due to restrictions on background activity starts.

Steps I Have Tried:

  1. Setting the App as the Default Launcher Added the following intent filters in AndroidManifest.xml to make the app act as a launcher:

    <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> However, the system does not automatically open my application when the TV starts.

  2. Using a Broadcast Receiver for BOOT_COMPLETED Implemented a BroadcastReceiver to detect when the TV boots and launch the application:

class BootReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
            val launchIntent = Intent(context, MainActivity::class.java)
            launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(launchIntent)
        }
    }
}

The receiver successfully logs the BOOT_COMPLETED event, but the app does not open.

  1. Using a Foreground Service with Broadcast Receiver Since Android 10+ blocks launching activities from the background, I started a foreground service from the BootReceiver to launch MainActivity:
class BootReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
            val serviceIntent = Intent(context, BootService::class.java)
            context.startForegroundService(serviceIntent)
        }
    }
}
class BootService : Service() {

    override fun onCreate() {
        super.onCreate()
        Log.d("BootService", "Foreground Service Started")

        startForeground(1, createNotification())

        startMainActivity()
    }

    private fun startMainActivity() {
        val intent = Intent(this, MainActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            val activityOptions = ActivityOptions.makeTaskLaunchBehind()
            startActivity(intent, activityOptions.toBundle())
        } else {
            startActivity(intent)
        }

        Log.d("BootService", "Starting MainActivity")

        // Stop service after launching the activity
        stopSelf()
    }

    override fun onBind(intent: Intent?): IBinder? = null

    private fun createNotification(): Notification {
        val channelId = "boot_service_channel"
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                channelId,
                "Boot Service",
                NotificationManager.IMPORTANCE_LOW
            )
            val manager = getSystemService(NotificationManager::class.java)
            manager.createNotificationChannel(channel)
        }

        return NotificationCompat.Builder(this, channelId)
            .setContentTitle("Launching TV App")
            .setContentText("Starting News Channel App...")
            .build()
    }
}

The foreground service successfully starts, but MainActivity still does not launch.

  1. Granted the Following Permissions in AndroidManifest.xml <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> <uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION"/> <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE"/>

Even after explicitly granting these permissions, the app still does not start automatically after a reboot.

Findings from Android Documentation According to the official Android documentation:

Foreground services are restricted from launching activities unless the app has user interaction: Android prevents activities from starting in the background without user interaction:

Additional Notes: I have tested the application on an Android TV Emulator (API 34). The BOOT_COMPLETED broadcast is received, and the foreground service successfully starts. However, MainActivity never opens automatically after the TV starts.

Sample Project:

Question: Is there any alternative method to achieve this functionality? How can we ensure that an Android TV application launches automatically on boot despite these background activity restrictions? Is there any exemption or workaround to allow launching an activity after BOOT_COMPLETED?

本文标签: broadcastreceiverHow to Automatically Launch an Application on Android TV RestartStack Overflow