android 通过NotificationListenerService获取通知栏内容

1、通过NotificationListenerService获取通知栏内容:

NotificationListenerService是系统提供的监听通知栏服务,我们需要继承此服务实现自己的逻辑,代码如下:

public class MyNotificationListenerService extends NotificationListenerService {
    @Override
    public void onListenerConnected() {
        super.onListenerConnected();
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        getNotifyData(sbn);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) {
        getNotifyData(sbn);
 }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {

    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {
        super.onNotificationRemoved(sbn, rankingMap);
    }

    @Override
    public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap, int reason) {
        super.onNotificationRemoved(sbn, rankingMap, reason);
    }

    private void getNotifyData(StatusBarNotification sbn){
        try {
            Bundle extras = sbn.getNotification().extras;
            String content = "";
            String title = extras.getString(Notification.EXTRA_TITLE,""); //通知标题
            content = extras.getString(Notification.EXTRA_TEXT,"");//通知内容
            
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

2、继承后的service使用方法:

首先,在Manifest文件中注册:

        
            
                
            
        

在要启动服务的地方先检查是否有权限,没有打开权限设置界面,方法如下:

    //打开设置界面
    private void checkPermission(){
      if (!isEnabled()) {
            startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
        }
    }


// 判断是否打开了通知监听权限
    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;
    }

拥有读取权限后启动service,由于这个service启动后进程杀掉不关闭的话再次启动则无法启动,所以采用以下方法启动:

     //先关闭再启动
    public static void toggleNotificationListenerService(Context context) {
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(
                new ComponentName(context, MyNotificationListenerService.class),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

        pm.setComponentEnabledSetting(
                new ComponentName(context, MyNotificationListenerService.class),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    }

 

你可能感兴趣的:(android)