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:
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.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.
- 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.
- 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
版权声明:本文标题:broadcastreceiver - How to Automatically Launch an Application on Android TV Restart? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744883460a2630346.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论