欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android  通知使用權(quán)(NotificationListenerService)的使用

 更新時(shí)間:2017年06月22日 17:13:19   投稿:lqh  
這篇文章主要介紹了Android 通知使用權(quán)(NotificationListenerService)的使用的相關(guān)資料,需要的朋友可以參考下

Android  通知使用權(quán)(NotificationListenerService)的使用

簡介

當(dāng)下不少第三方安全APP都有消息管理功能或者叫消息盒子功能,它們能管理過濾系統(tǒng)中的一些無用消息,使得消息欄更清爽干凈。其實(shí)此功能的實(shí)現(xiàn)便是使用了Android中提供的通知使用權(quán)權(quán)限。Android4.3后加入了通知使用權(quán)NotificationListenerService,就是說當(dāng)你開發(fā)的APP擁有此權(quán)限后便可以監(jiān)聽當(dāng)前系統(tǒng)的通知的變化,在Android4.4后還擴(kuò)展了可以獲取通知詳情信息。下面我們來看看NotificationListenerService的具體使用。

使用

新建一服務(wù)類,使它繼承NotificationListenerService,并實(shí)現(xiàn)兩個(gè)重要的方法:

@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中聲明此服務(wù)類,并必須聲明BIND_NOTIFICATION_LISTENER_SERVICE許可和意圖過濾器

android.service.notification.NotificationListenerService,還有我們在系統(tǒng)設(shè)置中通知使用權(quán)列表中看到的label標(biāo)簽:

<serviceandroid:name=".NotificationListener" 
     android:label="通知使用權(quán)測試程序" 
     android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"> 
  <intent-filter> 
    <actionandroid:name="android.service.notification.NotificationListenerService"/> 
  </intent-filter> 
 </service> 

OK,就這么簡單就可以完成APP監(jiān)聽系統(tǒng)通知欄的功能了。接下來,我們來看看NotificationListenerService類還提供了一些重要的方法:

StatusBarNotification[] sbns = getActiveNotifications();         // 返回當(dāng)前系統(tǒng)所有通知的數(shù)組 
cancelAllNotifications();                        // 刪除系統(tǒng)中所有可被清除的通知 
cancelNotification(String pkg, String tag, int id);           // 刪除具體某一個(gè)通知 

還有上面我們提到的兩個(gè)重要的重寫方法:

onNotificationRemoved(StatusBarNotification sbn);            // 通知被移除時(shí)回調(diào) 
onNotificationPosted(StatusBarNotification sbn);             // 增加一條通知時(shí)回調(diào) 

這兩個(gè)重要的回調(diào)方法它們的參數(shù)StatusBarNotification對象是當(dāng)前觸發(fā)變化通知的詳細(xì)信息。來看下StatusBarNotification的使用:

sbn.getId();                               // 返回通知對應(yīng)的id 
sbn.getNotification();                          // 返回通知對象 
sbn.getPackageName();                          // 返回通知對應(yīng)的包名 
sbn.getPostTime();                            // 返回通知發(fā)起的時(shí)間 
sbn.getTag();                              // 返回通知的Tag,如果沒有設(shè)置返回null 
sbn.isClearable();                            // 返回該通知是否可被清楚,是否為FLAG_ONGOING_EVENT、FLAG_NO_CLEAR 
sbn.isOngoing();                             // 返回該通知是否在正在運(yùn)行的,是否為FLAG_ONGOING_EVENT 

其中,getNotification()返回的通知對象,還可以詳細(xì)看到通知的其它相關(guān)信息,如:

Notification notification = sbn.getNotification(); 
notification.contentView;                        // 通知的RemoteViews 
notification.contentIntent;                       // 通知的PendingIntent 
notification.actions;                          // 通知的行為數(shù)組 
// Android4.4后還擴(kuò)展了可以獲取通知詳情信息 
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); 
} 

跳轉(zhuǎn)系統(tǒng)設(shè)置里的通知使用權(quán)頁面

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

判斷是否擁有通知使用權(quán)

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

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論