gpt4 book ai didi

android - NotificationCompat.Builder addAction 在 JellyBean 之前的机器人中?

转载 作者:行者123 更新时间:2023-11-29 17:54:17 25 4
gpt4 key购买 nike

我有一个带有附加操作的通知,因此用户可以通过单击主要通知转到主要 Activity 或单击次要(扩展)通知停止服务。这适用于后 JellyBean Androids (4.1+)。如何在旧系统(API 11+)中实现类似的功能?

       noti = new NotificationCompat.Builder(this)
.setContentTitle("service")
.setContentText("running")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendIntent)
.setPriority(Notification.PRIORITY_HIGH)
.addAction(R.drawable.ic_launcher, "Stop", pIntent)

.build();

startForeground(12345, noti);

最佳答案

来自文档:

Builder class for NotificationCompat objects. Allows easier control over all the flags, as well as help constructing the typical notification layouts.

On platform versions that don't offer expanded notifications, methods that depend on expanded notifications have no effect.

For example, action buttons won't appear on platforms prior to Android 4.1. Action buttons depend on expanded notifications, which are only available in Android 4.1 and later.

For this reason, you should always ensure that UI controls in a notification are also available in an Activity in your app, and you should always start that Activity when users click the notification. To do this, use the setContentIntent() method.

来自 addAction():

Add an action to this notification. Actions are typically displayed by the system as a button adjacent to the notification content. Action buttons won't appear on platforms prior to Android 4.1. Action buttons depend on expanded notifications, which are only available in Android 4.1 and later. To ensure that an action button's functionality is always available, first implement the functionality in the Activity that starts when a user clicks the notification (see setContentIntent()), and then enhance the notification by implementing the same functionality with addAction().

我找到了 this您可能会觉得有用的教程。

关于android - NotificationCompat.Builder addAction 在 JellyBean 之前的机器人中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20643054/

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