You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android:系统更新后发送APP重启通知是否支持配置?

嘿,这个需求确实很实用,但遗憾的是Android系统本身并没有提供直接的配置选项来自动触发这类通知。不过我们可以通过代码自己实现这个功能,下面是两种靠谱的方案:

方案1:监听系统重启广播 + 对比系统版本

这种思路是等系统更新完成重启后,检测系统版本是否发生变化,以此判断是否需要提示用户重启应用:

  • 首先,创建一个BroadcastReceiver来监听ACTION_BOOT_COMPLETED广播(系统重启完成后会发送这个广播)。
  • 在Receiver的逻辑里,先从SharedPreferences中读取上次保存的系统SDK版本号,再和当前的Build.VERSION.SDK_INT对比。
  • 如果版本号不一致,说明系统刚完成更新,此时就发送通知提示用户重启应用,同时更新SharedPreferences里保存的版本号。

代码示例(Kotlin):

class SystemUpdateReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        if (intent?.action == Intent.ACTION_BOOT_COMPLETED) {
            val prefs = context?.getSharedPreferences("AppSystemPrefs", Context.MODE_PRIVATE) ?: return
            val savedSDKVersion = prefs.getInt("LastSystemSDK", -1)
            val currentSDKVersion = Build.VERSION.SDK_INT

            if (currentSDKVersion != savedSDKVersion) {
                // 发送重启提示通知
                showRestartNotification(context)
                // 更新保存的版本号
                prefs.edit().putInt("LastSystemSDK", currentSDKVersion).apply()
            }
        }
    }

    private fun showRestartNotification(context: Context) {
        val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        // Android 8.0+ 需要创建通知渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                "system_update_channel",
                "系统更新提示",
                NotificationManager.IMPORTANCE_DEFAULT
            )
            notificationManager.createNotificationChannel(channel)
        }

        // 点击通知直接打开应用
        val launchIntent = context.packageManager.getLaunchIntentForPackage(context.packageName)
        val pendingIntent = PendingIntent.getActivity(
            context,
            0,
            launchIntent,
            PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )

        val notification = NotificationCompat.Builder(context, "system_update_channel")
            .setSmallIcon(R.drawable.ic_app_notification)
            .setContentTitle("系统已更新")
            .setContentText("为了确保应用运行正常,请重启我们的应用")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .build()

        notificationManager.notify(1001, notification)
    }
}

清单文件配置:

别忘了在AndroidManifest.xml里注册Receiver并声明权限:

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

<application ...>
    <receiver
        android:name=".SystemUpdateReceiver"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>
方案2:用WorkManager实现更可靠的检测

如果担心广播被系统的省电策略限制(比如Doze模式下广播可能延迟或不触发),可以用WorkManager来实现:

  • 在应用的Application类初始化时,启动一个OneTimeWorkRequest。
  • 在Worker的逻辑里,同样对比系统版本号,不一致就发送通知。
  • WorkManager会自动处理系统的各种限制,确保任务能在合适的时机执行,可靠性更高。
注意事项
  • 第一次启动应用时,要先把当前的系统版本号保存到SharedPreferences里,避免刚安装就触发通知。
  • Android 12及以上版本,PendingIntent需要添加FLAG_IMMUTABLE或FLAG_MUTABLE标记,否则会抛出异常。
  • 必须创建通知渠道,否则Android 8.0及以上版本的通知无法正常显示。

内容的提问来源于stack exchange,提问作者CanadaRunner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:11:33