在我的应用程序中,有在后台运行的服务。我想通知用户该服务正在运行。但是我需要用户不能删除通知-通过在通知栏中按清除按钮或将其刷出

这意味着我需要在通知区域上方显示我的通知

最佳答案

这是可行的,但实现方式取决于您开发的API级别。

对于低于11的API级别,可以设置Notification.FLAG_NO_CLEAR。可以这样实现:

// Create notification
Notification note = new Notification(R.drawable.your_icon, "Example notification", System.currentTimeMillis());

// Set notification message
note.setLatestEventInfo(context, "Some text", "Some more text", clickIntent);

// THIS LINE IS THE IMPORTANT ONE
// This notification will not be cleared by swiping or by pressing "Clear all"
note.flags |= Notification.FLAG_NO_CLEAR;

对于高于11的API级别,或使用Android Support Library时,可以这样实现:
Notification noti = new Notification.Builder(mContext)
    .setContentTitle("Notification title")
    .setContentText("Notification content")
    .setSmallIcon(R.drawable.yourIcon)
    .setLargeIcon(R.drawable.yourBigIcon)
    .setOngoing(true) // Again, THIS is the important line
    .build();

08-05 04:35
查看更多