Home  >  Article  >  How to automatically launch an Android app when turning on the TV?

How to automatically launch an Android app when turning on the TV?

王林
王林forward
2024-02-22 13:04:14407browse

php editor Xinyi brings you a java question and answer about automatically launching Android applications when turning on the TV. In actual development, sometimes it is necessary to implement such a function. This article will share a solution to help you easily realize this requirement. With the continuous development of technology, developers need to constantly learn new knowledge and improve their technical level. Let’s take a look at how to implement this feature!

Question content

I will add these permissions to the manifest

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

I added bootreceiver to the manifest:

<receiver
            android:name="com.portlmedia.streets.bootreceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:directbootaware="true">
                <action android:name="android.intent.action.boot_completed" />
                <action android:name="android.intent.action.locked_boot_completed" />
                <action android:name="android.intent.action.quickboot_poweron" />
                <action android:name="android.intent.action.reboot"/>
            </intent-filter>
        </receiver>

I created bootreceiver in my project:

public class BootReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Intent activityIntent = new Intent(context, MainActivity.class);
            context.startActivity(activityIntent);
        }
    }
}

My question is, if you can help me, how can I test it using the emulator in android studio?

I put a breakpoint in the onreceive method but it doesn't hit it when I start the application I also tried using a cold restart but nothing worked and I wanted to test if it actually works or maybe there is something wrong with my code?

Solution

Step 1: Manifest Code

Add permissions

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

Internal application class

<receiver
            android:name=".bootreceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <category android:name="android.intent.category.default" />

                <action android:name="android.intent.action.boot_completed" />
                <action android:name="android.intent.action.quickboot_poweron" />
                <action android:name="com.htc.intent.action.quickboot_poweron" />  
            </intent-filter>
        </receiver>

       <service
            android:name=".appstartservice"
            android:enabled="true"
            android:exported="true"
            android:stopwithtask="false" />

Step 2: Obtain the overlay permission granted by the user or

adb shell pm grant com.example.appstart android.permission.system_alert_window

Step 3: bootreceiver

class bootreceiver : broadcastreceiver() {

    override fun onreceive(context: context, intent: intent?) {
        log.i(tag, "onreceive: boot received ${intent?.action}")
        val serviceintent = intent(context, appstartservice::class.java)
        contextcompat.startforegroundservice(context, serviceintent)
    }

    companion object {
        private const val tag = "bootreceiver"
    }
}

Step 4: Service Code

class AppStartService: Service() {

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        return START_STICKY
    }

    override fun onCreate() {
        super.onCreate()

        startForeground(1, createNotification())

        GlobalScope.launch {
            withContext(Dispatchers.Main) {
                try {
                    val intent = Intent(this@AppStartService, MainActivity::class.java)
                    intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                    startActivity(intent)
                } catch (ex: Exception) {
                    Log.e(TAG, "onCreate: ", ex)
                }
            }
        }

    }

    private fun createNotification(): Notification {
        val serviceChannel = NotificationChannel(
            CHANNEL_ID,
            "${getString(R.string.app_name)} Service",
            NotificationManager.IMPORTANCE_DEFAULT
        )
        val manager = getSystemService(
            NotificationManager::class.java
        )
        manager.createNotificationChannel(serviceChannel)

        return NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("${getString(R.string.app_name)} Service")
            .setSilent(true)
            .setContentText("Please restart this device if this service is not running")
            .setSmallIcon(R.mipmap.ic_launcher)
            .build()
    }


    companion object {
        private const val TAG = "AppStartService"
        private const val CHANNEL_ID = "app-start-service"
    }

}

In my case, the bootreceiver class receives the boot completion operation. So, work for me! Make sure the boot completion action works on a specific device.

The above is the detailed content of How to automatically launch an Android app when turning on the TV?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Get json file in bukkitNext article:Get json file in bukkit