当前位置:   article > 正文

Android 通过广播设置开机启动APP_android应用可以通过自定义广播启动吗

android应用可以通过自定义广播启动吗

第一步写一个自定义广播 重写onReceive方法

class MyReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
       
            if(Intent.ACTION_BOOT_COMPLETED == intent.action){
                val  thisIntent = Intent(context,MainActivity::class.java)//需要启动的activity
                thisIntent.action="android.intent.action.MAIN"
                thisIntent.addCategory("android.intent.category.LAUNCHER")
                thisIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                context.startActivity(thisIntent)
            }     
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

在Manifest文件里静态注册 上面的广播

 <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter android:priority="1000">//优先值设置最大
                <!--.接收启动完成的广播-->
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这样当开机完成时 就会收到广播APP启动指定的app

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/308473
推荐阅读
相关标签
  

闽ICP备14008679号