安卓从服务开始活动

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

我有一个应用程序,允许视频通话。它的工作原理是使用firebase通知。流程是

1)push comes2)PushService启动我的CallService.3)CallService启动IncomingCallActivityby PendingIntent 。

pushService -> callService -> callActivity

如果应用在前台,或者应用被最小化,这种方法就会有效。但如果应用根本没有启动或被用户关闭,调用链就不起作用了。callActivity没有启动。有什么方法可以启动活动吗?

val intent = CallActivity.newIntent(this, someExtra)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)

 PendingIntent.getActivity(
                this,
                123,
                intent,
                PendingIntent.FLAG_ONE_SHOT
            ).send()
android android-service android-pendingintent
1个回答
1
投票

如果您的应用程序的目标是API级别29或更高,您不能启动一个 ActivityService 如果 Service 是在后台,并且应用程序没有任何现有的活动(如你所见,当应用程序没有运行时,情况就是这样)。这个限制是在API第29级中添加的,目的是减少后台应用对UI的干扰。请看 https:/developer.android.comguidecomponentsactivitiesbackground-start。 解释了允许背景的确切必要条件。Service 发动 Activity.

要验证这是否是你的问题,请将你的targetSDK改为28或更低,看看问题是否消失。

© www.soinside.com 2019 - 2024. All rights reserved.