android - 启动广播接收器在小米的 miui 中不起作用(Poco x3)
问题描述
我知道以前有人问过很多类似的问题,但无论我尝试什么解决方案,我都无法让它发挥作用。
我有一个广播接收器代码,如下所示。
class OnBootBroadcast : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
logD("onReceive() started -> intent action: [${intent?.action}]")
// this is only to test if on boot broadcast is working
context?.let {
val i = Intent()
i.setClass(it, MainActivity::class.java)
i.flags = Intent.FLAG_ACTIVITY_NEW_TASK;
it.startActivity(i);
}
// tried to test by adding notification as well, didn't show
// do stuff here
}
}
我的清单文件就像
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name=".CustomApplication"
android:allowBackup="true"
android:fullBackupContent="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".view.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".broadcast.NotificationBroadcast" />
<receiver
android:name=".broadcast.OnBootBroadcast"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
tools:node="remove" />
</application>
我知道像 MIUI 这样的中国定制 ROM 会杀死应用程序的后台服务。为了解决这个问题,我尝试了以下方法。
在“安全”>“管理应用程序”中打开“自动启动”这个修复了通过滑动关闭应用程序时工作管理器后台服务无法正常工作的问题。但没有解决广播问题。
在“设置”>“电池和性能”>“应用节电”中设置“无限制”
我正在尝试使用开机广播来重新添加一些警报管理器以获得准确的定时通知。如果有一些替代方案可以实现这一目标,那么该信息也将不胜感激。
提前致谢 :)
解决方案
最后,我不需要更改清单文件中的任何内容。只有 android.intent.action.BOOT_COMPLETED 很好,因为其余的隐式广播不在异常列表中。
主要问题是 MIUI 需要几分钟才能开始广播,我在日志中没有注意到这一点。当它启动时,由于测试代码是从后台进程启动一个活动,因此 MIUI 正在杀死它(根据日志,对于由 MIUI 启动的后台进程进行启动广播,启动前台 UI 是不启用的)。因此,在我删除活动启动代码后,进一步的日志也开始显示。
推荐阅读
- python - 调用函数后,GCP Composer/Airflow 无法识别 DAG
- r - 运行循环,将 if else 条件应用于数据框的子集(或完整的数据框)
- docker - 无法从 gitlab 私有注册表(openshift)中提取图像
- scala - 什么时候在 Spark 中执行 .repartition(Int AnyValue),在读取 Parquet 之后(或)在 Parquet 上运行计算之后?
- typescript - 打字稿扩展/修改现有接口 - Vue路由器元
- amazon-web-services - 如何指定显式架构 AWS Glue PySpark 并使用书签
- javascript - 如何使用 Xpath 获取异步脚本标签的内容
- python - 在pdf视图中执行js
- python - 适合数据点的角臂
- xamarin - MvvmCross 和使用 MvxContentView 导航