国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發實現判斷通知欄是否打開及前往設置頁面的方法

2019-10-22 18:16:52
字體:
來源:轉載
供稿:網友

本文實例講述了Android開發實現判斷通知欄是否打開及前往設置頁面的方法。分享給大家供大家參考,具體如下:

項目中用到日程提醒功能,如果應用的通知欄沒有打開,則需要提示用戶前去打開通知欄,判斷通知欄是否打開代碼如下:

private boolean isNotificationEnabled(Context context) {    String CHECK_OP_NO_THROW = "checkOpNoThrow";    String OP_POST_NOTIFICATION = "OP_POST_NOTIFICATION";    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 = (Integer) opPostNotificationValue.get(Integer.class);      return ((Integer) 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;}

返回值為true時,通知欄打開,false未打開。

以下代碼為前往設置頁面:

private void goToSet(){    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.BASE) {      // 進入設置系統應用權限界面      Intent intent = new Intent(Settings.ACTION_SETTINGS);      startActivity(intent);      return;    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {// 運行系統在5.x環境使用      // 進入設置系統應用權限界面      Intent intent = new Intent(Settings.ACTION_SETTINGS);      startActivity(intent);      return;    }}

注:測試功能時發現,若在應用設置中關閉app的通知欄/不勾選顯示通知,則Toast無法顯示

希望本文所述對大家Android程序設計有所幫助。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 河津市| 赣州市| 中阳县| 类乌齐县| 林口县| 双牌县| 北宁市| 通江县| 塔城市| 余干县| 新乡县| 合阳县| 金坛市| 佛学| 应用必备| 凤城市| 荔波县| 伊宁县| 西峡县| 景德镇市| 贡山| 潮安县| 张家川| 禄劝| 唐山市| 闵行区| 金华市| 永顺县| 长春市| 香港 | 淅川县| 商洛市| 固安县| 台州市| 红桥区| 攀枝花市| 新昌县| 五莲县| 松江区| 兰考县| 同德县|