gpt4 book ai didi

android - 前台服务通知需要几秒钟才能显示

转载 作者:行者123 更新时间:2023-12-05 05:32:23 26 4
gpt4 key购买 nike

前台服务通知在 Android 12 上显示太慢。使用 ContextCompat.startForegroundService(...)mContext.startForegroundService(...)。它仍会在 5-10 秒后显示。

这是我的代码示例:

private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Counting steps", NotificationManager.IMPORTANCE_DEFAULT);
channel.enableVibration(false);
channel.setSound(null, null);
channel.setShowBadge(false);
notificationManager.createNotificationChannel(channel);
}
}

onStartCommand 方法:

  @Override
public int onStartCommand(Intent intent, int flags, int startId) {

String input = intent.getStringExtra("numberOfSteps");
createNotificationChannel();

Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_IMMUTABLE);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Notification.Builder notificationBuilder = new Notification.Builder(this, CHANNEL_ID)
.setContentTitle("Counting steps")
.setContentText(input)
.setSmallIcon(R.drawable.ic_baseline_directions_walk_24)
.setContentIntent(pendingIntent);


startForeground(FOREGROUND_ID, notificationBuilder.build());
}

return START_STICKY;
}

如何快速启动或显示前台服务?

最佳答案

立即显示通知的服务

If a foreground service has at least one of the followingcharacteristics, the system shows the associated notificationimmediately after the service starts, even on devices that run Android12 or higher:

  • The service is associated with a notification that includes actionbuttons.
  • The service has a foregroundServiceType ofmediaPlayback, mediaProjection, or phoneCall.
  • The serviceprovides a use case related to phone calls, navigation, or mediaplayback, as defined in the notification's categoryattribute.
  • The service has opted out of the behaviorchange by passing FOREGROUND_SERVICE_IMMEDIATE intosetForegroundServiceBehavior() when setting up thenotification.
  • On Android 13 (API level 33) or higher, if the user denies thenotification permission, they still see notices related to foregroundservices in the Foreground Services (FGS) Task Manager but don't seethem in the notification drawer.

    参见:Services that show a notification immediately

    Java 示例代码:

    Notification.Builder notificationBuilder = new Notification.Builder(this, CHANNEL_ID)
    .setContentTitle("Counting steps")
    .setContentText(message)
    .setSmallIcon(R.drawable.your_cool_icon)
    .setContentIntent(pendingIntent);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
    notificationBuilder.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE);
    }

    Kotlin 示例:

    val notificationBuilder: Notification.Builder =
    Notification.Builder(this, CHANNEL_ID)
    .setContentTitle("Notification title")
    .setContentText("Content title")
    .setSmallIcon(R.drawable.your_cool_icon)
    .setContentIntent(pendingIntent)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    notificationBuilder.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE)
    }

    关于android - 前台服务通知需要几秒钟才能显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74151910/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com