@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");
}
}
<serviceandroid:name=".NotificationListener"
android:label="通知使用权测试程序"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<actionandroid:name="android.service.notification.NotificationListenerService"/>
</intent-filter>
</service>
StatusBarNotification[] sbns = getActiveNotifications(); // 返回当前系统所有通知的数组 cancelAllNotifications(); // 删除系统中所有可被清除的通知 cancelNotification(String pkg, String tag, int id); // 删除具体某一个通知
onNotificationRemoved(StatusBarNotification sbn); // 通知被移除时回调 onNotificationPosted(StatusBarNotification sbn); // 增加一条通知时回调
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
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;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有