gpt4 book ai didi

java - 使通知可点击且不可销毁

转载 作者:行者123 更新时间:2023-12-01 18:44:14 25 4
gpt4 key购买 nike

我尝试创建一个通知,通过单击该通知来启动 Activity ,并且您无法将其滑开。

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("DroidSchool")
.setContentText("DroidSchool l\u00E4uft im Hintergrund...");

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_FROM_BACKGROUND);

NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

int mId = 1234567890;
mNotificationManager.notify(mId, mBuilder.build());

使用上面的代码,会显示通知,但当我单击它时没有任何反应,您可以将其滑开。

最佳答案

要保留通知,这样用户就无法以任何方式点击它,请添加此标志

Notification mNotification = mBuilder.build();
notification.flags = Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(mId, notification);

要启动 Activity ,您必须为您的 Intent 使用此标志

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

关于java - 使通知可点击且不可销毁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18535847/

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