Android RECEIVE_BOOT_COMPLETED广播不生效问题求助
Android RECEIVE_BOOT_COMPLETED广播不生效问题求助
我用flutter create reboot新建了一个基础Flutter项目,想要实现手机重启后App能收到广播,这样就能重启后台任务(目前先只打日志测试效果)。但现在不管是日志输出还是后台任务都没反应,我是不是漏了什么关键配置或者步骤?
AndroidManifest.xml 配置
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <application android:label="reboot" android:name="${applicationName}" android:icon="@mipmap/ic_launcher"> <activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop" android:taskAffinity="" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize"> <meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" /> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <meta-data android:name="flutterEmbedding" android:value="2" /> <receiver android:name=".BootReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <uses-library android:name="org.apache.http.legacy" android:required="false"/> </application> <queries> <intent> <action android:name="android.intent.action.PROCESS_TEXT"/> <data android:mimeType="text/plain"/> </intent> </queries> </manifest>
BootReceiver 实现
// /android/app/src/main/kotlin/com.example.reboot/BootReceiver package com.example.reboot import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.util.Log class BootReceiver : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { Log.d("BootReceiverTest", "onReceive triggered with action: ${intent?.action}") } }
测试步骤
我用了真实设备测试(小米、LG Nexus、Pixel),具体步骤如下:
- 用USB把手机连到电脑,执行
flutter run - 打开App,确认能正常运行
- 关闭App,然后完全重启手机
- 重启后登录手机,开启USB文件传输权限
- 执行
adb logcat | grep "onReceive triggered",但完全看不到我的App输出的日志
内容来源于stack exchange
相关产品推荐
相关产品推荐

