android - 安卓 : how to create a non erasable custom notification in the statusbar?

标签 android notifications statusbar

我只想在状态栏上制作一个自定义通知,该通知在用户交互期间仍然存在。

我使用这个代码:

    Notification notification = new Notification(getBatteryStateResource(percent,status), "notify",System.currentTimeMillis());
    notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
    notification.contentView = new RemoteViews(mContext.getPackageName(), R.layout.statusbar);
    Intent intent = new Intent(mContext, Activity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
    notification.contentIntent=contentIntent;
    notification.contentView.setOnClickPendingIntent(R.id.imageView, anotherContentIntent);
    NotificationManager nm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.notify(42, notification);

此代码仅适用于 Android 4 智能手机。在平板电脑上,每次我点击通知的 imageView 时,系统都会删除通知。

相反,在 Android 2.3 和 2.2 上,“anotherContentIntent”不会启动并且仅启动“contentIntent”,为什么?

非常感谢......

最佳答案

使用FLAG_NO_CLEAR除了FLAG_ONGOING_EVENT

关于android - 安卓 : how to create a non erasable custom notification in the statusbar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10371146/

相关文章:

flutter - 如何在 Flutter 中更改状态栏和应用栏颜色?

android - MadiaStreamTrack 缩放功能未在 Android WebView 中返回,但适用于 Chrome

android - 我应该用 Sqlite 保存数据吗?

java - 如何阻止通知面板隐藏?

java - 如何根据服务器响应动态更改 View 的值?

android - 状态栏上的图像叠加

Android 库项目并且必须手动刷新

java - 格式化多个数据以插入到 sqlite 中的单列中

swift - 如何从通知中访问 userInfo 属性?

java - 工具栏覆盖状态栏