如何将 Wear OS OnGoingActivity 从服务带回前台

问题描述 投票:0回答:1

Wear OS 应用程序设计为在活动时以随机间隔显示消息。为了实现这一点,应用程序利用后台服务与链接到正在进行的活动的通知配对。

  • 启动活动。
  • 单击以启用“活动”状态,建立与服务的连接,finish() 活动
  • 形成服务,构建与通知相关的持续活动。
  • 在 Wear OS 主屏幕上将通知显示为正在进行的活动,确认应用程序的持续运行。
  • 在指定的延迟后,将 Activity 带到前台以显示消息。

当单击通知图标或在最近的应用程序中时,活动将置于最前面。

我想从服务中的 postDelayed 处理程序执行相同的操作,所以我尝试这样做:

    handler.postDelayed({
            Log.d(TAG, "Post delayed fired")
            val intent = Intent(this, MainActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
            val pIntent = PendingIntent.getActivity(
                this, 0, intent, PendingIntent.FLAG_IMMUTABLE
            )
            pIntent.send()
        }, delayMillis)

这导致了这个日志:

2024-01-23 11:18:14.220   540-4470  ActivityTaskManager     system_process                       I  START u0 {flg=0x20000 cmp=com.pixtogram.wear.posipulse/.presentation.MainActivity} from uid 10088
2024-01-23 11:18:14.222   540-4470  ActivityTaskManager     system_process                       W  Background activity start [callingPackage: com.pixtogram.wear.posipulse; callingUid: 10088; appSwitchState: 2; isCallingUidForeground: false; callingUidHasAnyVisibleWindow: false; callingUidProcState: FOREGROUND_SERVICE; isCallingUidPersistentSystemProcess: false; realCallingUid: 10088; isRealCallingUidForeground: false; realCallingUidHasAnyVisibleWindow: false; realCallingUidProcState: FOREGROUND_SERVICE; isRealCallingUidPersistentSystemProcess: false; originatingPendingIntent: PendingIntentRecord{48f29d com.pixtogram.wear.posipulse startActivity (allowlist: b7bf5d9:+30s0ms/0/NOTIFICATION_SERVICE/NotificationManagerService)}; allowBackgroundActivityStart: false; intent: Intent { flg=0x20000 cmp=com.pixtogram.wear.posipulse/.presentation.MainActivity }; callerApp: ProcessRecord{752085a 13164:com.pixtogram.wear.posipulse/u0a88}; inVisibleTask: false]
2024-01-23 11:18:14.227   540-4470  ActivityTaskManager     system_process                       W  startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { flg=0x20000 cmp=com.pixtogram.wear.posipulse/.presentation.MainActivity }

当点击通知时会记录以下内容:

2024-01-23 11:20:50.119   540-4470  ActivityTaskManager     system_process                       I  START u0 {flg=0x20000 cmp=com.pixtogram.wear.posipulse/.presentation.MainActivity} from uid 10088
2024-01-23 11:20:50.123   540-4470  ActivityTaskManager     system_process                       W  startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { flg=0x20000 cmp=com.pixtogram.wear.posipulse/.presentation.MainActivity }

我无法理解为什么在这种情况下该活动会在后台启动,提前感谢您的帮助。

android wear-os
1个回答
0
投票

如日志语句中所述,当从服务(以及本身不是 Activity 上下文的任何其他上下文)启动 Activity 时,您需要在意图上设置

Intent.FLAG_ACTIVITY_NEW_TASK
标志。

尝试像这样构建您的 Intent 和 PendingIntent:

val intent = Intent(this, MainActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

val pIntent = PendingIntent.getActivity(
    this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
© www.soinside.com 2019 - 2024. All rights reserved.