亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android如何獲取系統(tǒng)通知的開(kāi)啟狀態(tài)詳解

 更新時(shí)間:2017年08月08日 10:31:52   投稿:daisy  
這篇文章主要給大家介紹了關(guān)于Android如何獲取系統(tǒng)通知開(kāi)啟狀態(tài)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起看看吧啊。

前言

大家應(yīng)該都有所體會(huì),平常在android應(yīng)用中,有時(shí)候會(huì)用到系統(tǒng)通知是否開(kāi)啟的狀態(tài),以便進(jìn)行下一步操作,所以,獲取到狀態(tài)是很有必要的,之前一直苦于找不到合適的方法來(lái)解決,因?yàn)楫吘股婕暗较到y(tǒng),不好辦,今日看到大神支招,試了一下,很好用,話(huà)不多少了,來(lái)一起看看詳細(xì)的介紹吧。

有圖有真相,首先到設(shè)置里邊關(guān)閉該應(yīng)用的通知開(kāi)關(guān):

然后在應(yīng)用中,點(diǎn)擊按鈕,獲取狀態(tài):

這時(shí)候,回到設(shè)置里,打開(kāi)通知按鈕:

再次點(diǎn)擊應(yīng)用中的測(cè)試按鈕,可以看到,通知已經(jīng)可用了:

代碼量很少,但是很精辟,就一個(gè)工具類(lèi),用到了java反射原理:

public class NotificationsUtils {
 private static final String CHECK_OP_NO_THROW = "checkOpNoThrow";
 private static final String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";
 public static boolean isNotificationEnabled(Context context) {
  AppOpsManager mAppOps = (AppOpsManager)
  context.getSystemService(Context.APP_OPS_SERVICE);
  ApplicationInfo appInfo = context.getApplicationInfo();
  String pkg = context.getApplicationContext().getPackageName();
  int uid = appInfo.uid;
  Class appOpsClass = null; /* Context.APP_OPS_MANAGER */
  try {
   appOpsClass = Class.forName(AppOpsManager.class.getName());
   Method checkOpNoThrowMethod = appOpsClass.getMethod(CHECK_OP_NO_THROW, Integer.TYPE, Integer.TYPE, String.class);
   Field opPostNotificationValue = appOpsClass.getDeclaredField(OP_POST_NOTIFICATION);
   int value = (int)opPostNotificationValue.get(Integer.class);
   return ((int)checkOpNoThrowMethod.invoke(mAppOps,value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
  }
  catch (ClassNotFoundException e) {
   e.printStackTrace();
  } catch (NoSuchMethodException e) {
   e.printStackTrace();
  } catch (NoSuchFieldException e) {
   e.printStackTrace();
  } catch (InvocationTargetException e) {
   e.printStackTrace();
  } catch (IllegalAccessException e) {
   e.printStackTrace();
  }
  return false;
 }
}

總結(jié)

好了,以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論