- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
前台服务通知在 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/
我是一名优秀的程序员,十分优秀!