清理手机notification 实现过程

参考http://blog.csdn.net/cankingapp/article/details/50858229
NotificationListenerService 非常容易的获取通知回调的相关信息
本次就用NotificationListenerService(NLS)进行通知的清理

通知的新增和删除,都会回调你注册的NLS里的方法,这些信息可以通过StatusBarNotification类对象来获取。

重点:系统开启通知读取服务后,系统就会对APP有个保活功能,当被系统或是第三方软件kill后,系统会将你的应用重启。很多厂商会利用这点做一些坏坏的事情。并且国内各大杀毒,清理软件都有开启该功能。

NotificationListenerService主要方法(成员变量):
删除通知时会回调onNotificationRemoved, 新增通知或是更新时会回调onNotificationPosted

cancelAllNotifications() :删除系统中所有 可被清除 的通知;
cancelNotification(String pkg, String tag, int id) :删除具体某一个通知;
getActiveNotifications() :返回当前系统所有通知到StatusBarNotification[]的列表;
onNotificationPosted(StatusBarNotification sbn) :当系统收到新的通知后出发回调;
onNotificationRemoved(StatusBarNotification sbn) :当系统通知被删掉后出发回调;

StatusBarNotification类详解
StatusBarNotification,多进程传递对象,所有通知信息都会在这个类中通过Binder传递过来.
内部几个重要的方法如下:


getId():返回通知对应的id;
getNotification():返回通知对象;
getPackageName():返回通知对应的包名;
getPostTime():返回通知发起的时间;
getTag():返回通知的Tag,如果没有设置返回null;
isClearable():返回该通知是否可被清楚,FLAG_ONGOING_EVENT、FLAG_NO_CLEAR;
isOngoing():检查该通知的flag是否为FLAG_ONGOING_EVENT;
其中,我们通过getNotification()可以得到Notification对象,Notification是我们比较熟悉的类了,我们可以得到通知具体内容甚至可以还原RemoteViews到我们的本地view上。

使用方法

  1. 新建一个类并继承自NotificationListenerService
public class NLService extends NotificationListenerService {
        @Override
        public void onNotificationPosted(StatusBarNotification sbn) {
        }

        @Override
        public void onNotificationRemoved(StatusBarNotification sbn) {
        }
    }
  1. 在AndroidManifest.xml中注册Service并声明相关权限;
 <service android:name=".NLService"
          android:label="@string/service_name"
          android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
     <intent-filter>
         <action android:name="android.service.notification.NotificationListenerService" />
     </intent-filter>
   </service>
  1. 开启NotificationMonitor的监听功能;
if (!isEnabled()) {
                Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
                startActivity(intent);
            } else {
                Toast.makeText(this, "已开启服务权限", Toast.LENGTH_LONG).show();
            }
 private boolean isEnabled() {
        String pkgName = getPackageName();
        final String flat = Settings.Secure.getString(getContentResolver(),
                ENABLED_NOTIFICATION_LISTENERS);
        if (!TextUtils.isEmpty(flat)) {
            final String[] names = flat.split(":");
            for (int i = 0; i < names.length; i++) {
                final ComponentName cn = ComponentName.unflattenFromString(names[i]);
                if (cn != null) {
                    if (TextUtils.equals(pkgName, cn.getPackageName())) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

github https://github.com/CankingApp/notifymrg
接下来就要自己实现了~
希望不要遇到坑

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,744评论 25 709
  • 秋意好,无边落木尽纷飞;万花谢,多情残荷空垂泪。 凉风过,湖裙掀,碎她江月引人醉;蠢蠢心,千百对,恰似了繁星点点我...
    逸之阅读 454评论 5 1
  • 早上的时候,和神立了约,要写一篇最近的见证。从没有写过见证,总觉得写出来的感动都很小,无法传达出对神的感恩。但是,...
    纯子404阅读 175评论 0 0
  • 最近遇到用AFN做接口的网络请求 发现 AFN请求失败报错,然后加个断点 看了一下 发现是因为 NSLocaliz...
    咋了爸爸阅读 4,503评论 1 3
  • 冥想一般是指沉思或反省自我,大脑本质和更高境界的行为,可以帮助达到把注意力集中在当下时刻的能力状态。这种状态不...
    大侠展翅阅读 1,015评论 0 3