通知待定意图存在问题

3

我想启动一个不可取消的持续通知,但无论我尝试什么,它都是可取消的。

代码:

    NotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int icon = R.drawable.btn_rating_star_on_pressed_holo_light;
    long when = System.currentTimeMillis();

    notification = new Notification(icon, getString(R.string.notificationMSG), when);
    notification.ledARGB = 999;

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, Notification.FLAG_ONGOING_EVENT + Notification.FLAG_NO_CLEAR + Notification.FLAG_SHOW_LIGHTS);

    notification.setLatestEventInfo(context, getString(R.string.app_name), getString(R.string.notificationMSG), contentIntent);

    notificationManager.notify(Global.NOTIFICATION_ID,notification);

我还想知道如何在启动它的活动完成后让它消失。尝试过以下方法,但仅在某些情况下有效。

@Override
protected void onDestroy(){
    notificationManager.cancel(Global.NOTIFICATION_ID);
    super.onDestroy();
}

没起作用。好像没有任何标志被激活。LED 也不工作。 - Moises Jimenez
1个回答

2
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;

由于某些愚蠢的原因,我们不能直接在PendingIntent中设置标志,而是需要通过这种方式进行设置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接