Android通知使用权(NotificationListenerService)的使用

简介

当下不少第三方安全APP都有消息管理功能或者叫消息盒子功能,它们能管理过滤系统中的一些无用消息,使得消息栏更清爽干净。其实此功能的实现便是使用了Android中提供的通知使用权权限。Android4.3后加入了通知使用权NotificationListenerService,就是说当你开发的APP拥有此权限后便可以监听当前系统的通知的变化,在Android4.4后还扩展了可以获取通知详情信息。下面我们来看看NotificationListenerService的具体使用。

使用

新建一服务类,使它继承NotificationListenerService,并实现两个重要的方法:

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public class NotificationListener extends NotificationListenerService {
    privatestatic final String TAG = "NotificationListener";
 
    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
       Log.i(TAG,"Notification removed");
    }
 
    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
       Log.i(TAG, "Notification posted");
    }
}

AndroidManifest.xml中声明此服务类,并必须声明BIND_NOTIFICATION_LISTENER_SERVICE许可和意图过滤器android.service.notification.NotificationListenerService,还有我们在系统设置中通知使用权列表中看到的label标签:

<serviceandroid:name=".NotificationListener"
         android:label="通知使用权测试程序"
         android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
    <intent-filter>
        <actionandroid:name="android.service.notification.NotificationListenerService"/>
    </intent-filter>
 </service>

OK,就这么简单就可以完成APP监听系统通知栏的功能了。接下来,我们来看看NotificationListenerService类还提供了一些重要的方法:

StatusBarNotification[] sbns = getActiveNotifications();                 // 返回当前系统所有通知的数组
cancelAllNotifications();                                                // 删除系统中所有可被清除的通知
cancelNotification(String pkg, String tag, int id);                      // 删除具体某一个通知

还有上面我们提到的两个重要的重写方法:

onNotificationRemoved(StatusBarNotification sbn);                        // 通知被移除时回调
onNotificationPosted(StatusBarNotification sbn);                         // 增加一条通知时回调

这两个重要的回调方法它们的参数StatusBarNotification对象是当前触发变化通知的详细信息。来看下StatusBarNotification的使用:

sbn.getId();                                                             // 返回通知对应的id
sbn.getNotification();                                                   // 返回通知对象
sbn.getPackageName();                                                    // 返回通知对应的包名
sbn.getPostTime();                                                       // 返回通知发起的时间
sbn.getTag();                                                            // 返回通知的Tag,如果没有设置返回null
sbn.isClearable();                                                       // 返回该通知是否可被清楚,是否为FLAG_ONGOING_EVENT、FLAG_NO_CLEAR
sbn.isOngoing();                                                         // 返回该通知是否在正在运行的,是否为FLAG_ONGOING_EVENT

其中,getNotification()返回的通知对象,还可以详细看到通知的其它相关信息,如:

Notification notification = sbn.getNotification();
notification.contentView;                                                // 通知的RemoteViews
notification.contentIntent;                                              // 通知的PendingIntent
notification.actions;                                                    // 通知的行为数组
// Android4.4后还扩展了可以获取通知详情信息
if (Build.VERSION.SDK_INT >Build.VERSION_CODES.JELLY_BEAN_MR2) {
         Bundle extras = notification.extras;
         String notificationTitle = extras.getString(Notification.EXTRA_TITLE);
         int notificationIcon = extras.getInt(Notification.EXTRA_SMALL_ICON);
         Bitmap notificationLargeIcon = ((Bitmap)extras.getParcelable(Notification.EXTRA_LARGE_ICON));
         CharSequence notificationText = extras.getCharSequence(Notification.EXTRA_TEXT);
         CharSequence notificationSubText = extras.getCharSequence(Notification.EXTRA_SUB_TEXT);
}

跳转系统设置里的通知使用权页面

private boolean gotoNotificationAccessSetting(Contextcontext) {
    try {
        Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        return true;
    } catch(ActivityNotFoundException e) {
        try {
           Intent intent = new Intent();
           intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           ComponentName cn = new ComponentName("com.android.settings","com.android.settings.Settings$NotificationAccessSettingsActivity");
           intent.setComponent(cn);
           intent.putExtra(":settings:show_fragment", "NotificationAccessSettings");
           context.startActivity(intent);
           return true;
        } catch(Exception ex) {
           ex.printStackTrace();
        }
        return false;
    }
}

判断是否拥有通知使用权

private boolean notificationListenerEnable() {
    boolean enable = false;
    String packageName = getPackageName();
    String flat= Settings.Secure.getString(getContentResolver(),"enabled_notification_listeners");
    if (flat != null) {
        enable= flat.contains(packageName);
    }
    return enable;
}

 

  • 6
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
以下是使用 Android Studio 创建通知的示例代码: 1. 在 `AndroidManifest.xml` 文件中添加通知权限: ```xml <uses-permission android:name="android.permission.VIBRATE"/> ``` 2. 在布局文件中创建一个按钮并设置其 `onClick` 事件: ```xml <Button android:id="@+id/notification_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Notification" android:onClick="sendNotification"/> ``` 3. 在 Activity 中实现 `sendNotification` 方法,该方法创建并发送通知: ```java public void sendNotification(View view) { // 创建通知 NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "my_channel") .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT); // 发送通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(0, builder.build()); } ``` 上述代码中,我们创建了一个包含小图标、标题和内容的通知,并将其发送到系统通知栏中。注意,我们需要指定一个通知渠道,这可以在应用启动时进行设置。 4. 在应用启动时创建通知渠道: ```java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 创建通知渠道 CharSequence name = getString(R.string.channel_name); String description = getString(R.string.channel_description); int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel("my_channel", name, importance); channel.setDescription(description); // 注册通知渠道 NotificationManager notificationManager = getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } ``` 上述代码中,我们检查当前 Android 版本是否支持通知渠道,如果支持,则创建一个默认级别的通知渠道,并在系统中注册该渠道。 现在,当用户点击按钮时,应用将创建并发送一个通知到系统通知栏中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值