gpt4 book ai didi

android - 操作按钮未显示在通知中

转载 作者:行者123 更新时间:2023-11-30 03:14:47 27 4
gpt4 key购买 nike

我想在通知栏中显示操作按钮。通知有效,但我看不到任何按钮。我已经在 Internet 上阅读了 Android 开发人员文档和不同的示例,但我没有发现我的代码有什么大的不同。我做了如下操作:

public void showNotification(Context context) {
NotificationManager mNotifyMgr = (NotificationManager);
context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder = new NotificationCompat.Builder(context);

Intent prevIntent = new Intent(mContext, PlayAudioService.class);
prevIntent.setAction(ACTION_PREV);
PendingIntent piPrev = PendingIntent.getService(mContext, 0, prevIntent, 0);

Intent playIntent = new Intent(mContext, PlayAudioService.class);
playIntent.setAction(ACTION_PLAY);
PendingIntent piPlay = PendingIntent.getService(mContext, 0, playIntent, 0);

Intent nextIntet = new Intent(mContext, PlayAudioService.class);
nextIntet.setAction(ACTION_NEXT);
PendingIntent piNext = PendingIntent.getService(mContext, 0, nextIntet, 0);

mBuilder.setSmallIcon(smallIcon)
.setContentTitle("Title")
.setContentText("Text")
.setTicker("Ticker")
.setWhen(0)
//.setStyle(new NotificationCompat.BigTextStyle().bigText(title))
.addAction (R.drawable.ic_previous, "Prev", piPrev)
.addAction (R.drawable.ic_play, "Play", piPlay)
.addAction (R.drawable.ic_next, "Next", piNext);

Intent notifyIntent = new Intent(context, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

PendingIntent piNotify =
PendingIntent.getActivity(
mContext,
0,
notifyIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(piNotify);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}

Activity 中:

showNotification(this);

我的 Android 目标 SDK 版本 15,我使用的是最新版本的支持库 v4。我错过了什么,没有正确理解什么?

我们将不胜感激。

最佳答案

API 16 中引入了通知操作.

这意味着您应该将目标 SDK 设置为 16 或更高版本。

但是,只有 Jelly Bean 及更高版本可以利用通知操作 - 请务必在这些平台上测试此功能。

NotificationCompat.Builder 负责生成与其运行的任何 API 兼容的通知,因此如果您要支持 Ice Cream Sandwich 及更早版本,请继续使用它。如果没有,只需使用 Notification.Builder 就足够了(当然是在更改目标 SDK 之后)。

关于android - 操作按钮未显示在通知中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20311245/

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