不可拆卸的通知 [英] non removable notification

查看:133
本文介绍了不可拆卸的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,有后台运行的服务。我想通知用户该服务正在运行。但我需要一个用户不能删除通知 - 由pressing清晰的按钮或刷卡出来,在通知栏

In my app, there is service running on background. I want to notify user that the service is running. But I need that user cannot delete the notification - by pressing clear button or by swipe it out, in notification bar

这意味着我需要证明我的上述通知区域的通知

It means I need to show my notification above Notification area

推荐答案

这是可能的,但要实现它的方法取决于API级别您开发的。

This is possible but the way you implement it depends on the API level you develop for.

对于低于11 API级别,您可以设置<一个href="http://developer.android.com/reference/android/app/Notification.html#FLAG_NO_CLEAR">Notification.FLAG_NO_CLEAR.这可以这样来实现:

For API levels below 11, you can set Notification.FLAG_NO_CLEAR. This can be implemented like this:

// 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;

有关API水平高于11,或使用 Android的支持库,可以实现它是这样的:

For API levels above 11, or when using the Android Support Library, one can implement it like this:

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();

这篇关于不可拆卸的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆