gpt4 book ai didi

android - 如何删除通知操作

转载 作者:太空狗 更新时间:2023-10-29 13:22:09 26 4
gpt4 key购买 nike

我有一个创建/更新通知的方法:

private void createNotification(Boolean removeAction) {
getButtonPendingIntent().cancel();

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My App")
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_MAX);

NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle("My App");

if (removeAction) {
String msg = "Some message";
bigTextStyle.bigText(msg);
builder.setContentText(msg);
// this will remove button but leaves an empty space where the button existed
builder.addAction(0, null, null);
} else {
String msg = "You are logged in. Slide down to logout.";
bigTextStyle.bigText(msg);
builder.setContentText(msg);
builder.addAction(R.drawable.some_icon, "My Button", getButtonPendingIntent());
}

builder.setStyle(bigTextStyle);

// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(100, builder.build());
}

有时我需要删除我在通知中创建的操作。如果我只是创建一个新构建并通知一个没有操作的新事件,操作按钮仍然存在。

我在这段代码中添加:

builder.addAction(0, null, null);

这会删除操作按钮,但在通知屏幕中仍然会在该按钮所在的位置留下空白区域。是否有“正确”的方法从通知中删除操作及其使用的空间?

最佳答案

自上次回答以来已经花费了很多时间。但我认为对某些人来说它可能有用。在 NotificationCompat.Builder 中有一个 ArrayList - mActions。我通过调用 myNotificationBuilder.mActions.clear() 清除了这个 Arraylist。它帮助我从通知中删除操作按钮而无需重新创建它。

关于android - 如何删除通知操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27305728/

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