Android:如何在状态栏中创建不可擦除的自定义通知?

时间:2012-04-29 09:38:09

标签: 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”,为什么?

非常感谢....

1 个答案:

答案 0 :(得分:2)

除了FLAG_ONGOING_EVENT之外,请使用FLAG_NO_CLEAR