定时器的前台服务类型

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

当用户在锻炼时关闭屏幕时,我的锻炼应用程序使用前台服务来保持准确的计时器运行。有倒计时和无限期计时选项。这在 Android 14 之前的设备上效果很好。Android 14 要求我命名服务类型。如果没有这个,我会得到一个

android.app.MissingForegroundServiceTypeException
,但是当我查看各种服务类型时,我没有看到任何适合用例的服务类型。它不是适合
health
的传感器,也不是适合
systemExempted
的精确计时器,因为计数是不确定的,并且
specialUse
周围的限制让我认为它不可能被接受。

有谁知道我应该使用哪种类型。启动服务的代码如下:

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle(exerciseNameStr + " Timer Running")
                .setSmallIcon(exerciseIcons.getResourceId(iconId, 0))
                .setContentIntent(pendingIntent)
                .setChronometerCountDown(!countUpwards)
                .setUsesChronometer(true)
                .setTimeoutAfter(counterMsec + 1000)
                .setWhen(time)
                .build();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
        {
            startForeground(100, notification, FOREGROUND_SERVICE_TYPE_???);
        }
        else
        {
            startForeground(100, notification);   // <-- this code raises an exception for Android 14
        }

是否有另一种方法来实现这种类型的计时器,在通知上显示勾号?

android timer notifications foreground-service
1个回答
0
投票

没有答案,我选择了

specialUse
,如下:

AndroidManifest.xml:

<manifest
  <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
...
  <application>
...
    <service
        android:name=".CountService"
        android:foregroundServiceType="specialUse"
        android:exported="false">
        <property
            android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
            android:value="Foreground service needed to [fill in details for use case here]..."/>
    </service>
  </application>
</manifest

除此之外,我还必须在 Google Play 控制台中添加一个链接,其中包含一个视频,显示该应用程序正在运行,以证明用例的合理性。经过长时间的审查,一切都很好。

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