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

Android智能手表后台触发表盘弹窗式庆祝Activity求助

解决方案

1. 替换前台服务通知为普通通知

startForeground() 是用于绑定服务到状态栏的常驻通知,这类通知不会主动触发弹窗。你需要改用 NotificationManager.notify() 发送普通通知:

val notificationManager = getSystemService(NotificationManager::class.java)
notificationManager.notify(NOTIFICATION_ID_2, createNotification("TestNotification","This is a test notification"))

2. 适配Wear OS弹窗逻辑:添加全屏Intent

Wear OS 对需要弹窗的高优先级通知,必须配置 FullScreenIntent 而非普通的 ContentIntent。修改你的通知创建方法:

private fun createNotification(title: String, text: String): Notification {
    val fullScreenIntent = Intent(this, ThresholdAchieved::class.java).apply {
        flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    }
    val fullScreenPendingIntent = PendingIntent.getActivity(
        this, 0, fullScreenIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
    )

    val channel = NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH).apply {
        description = "计步达标提醒渠道"
        setShowBadge(true)
    }
    getSystemService(NotificationManager::class.java).createNotificationChannel(channel)

    return NotificationCompat.Builder(this, CHANNEL_ID)
        .setContentTitle(title)
        .setContentText(text)
        .setPriority(NotificationCompat.PRIORITY_MAX)
        .setCategory(NotificationCompat.CATEGORY_ALARM)
        .setSmallIcon(R.drawable.splash_icon)
        .setFullScreenIntent(fullScreenPendingIntent, true) // 核心配置:触发弹窗
        .setAutoCancel(true)
        .build()
}

3. 配置弹窗Activity的手表适配主题

在 AndroidManifest.xml 中给 ThresholdAchieved Activity 设置手表专用的弹窗主题,避免全屏覆盖表盘:

<activity
    android:name=".ThresholdAchieved"
    android:theme="@style/Theme.Wear.Light.Dialog"
    android:exported="false" />

若使用Material Components库,也可替换为:

android:theme="@style/Theme.MaterialComponents.Dialog.Alert"

4. 补充权限检查

针对Android 10+设备,需添加悬浮窗权限:

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

部分Wear OS设备需要手动在应用设置中开启「显示在其他应用上层」权限。

5. 直接启动弹窗Activity的替代方案

如果通知方式仍不生效,可跳过通知,直接在计步达标时启动Dialog风格Activity:

// 计步达标时执行
val intent = Intent(this, ThresholdAchieved::class.java).apply {
    flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
}
startActivity(intent)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 06:55:14