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

首頁 > 系統 > Android > 正文

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

2019-12-12 01:16:59
字體:
來源:轉載
供稿:網友

本文實例講述了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開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 荥阳市| 军事| 滦平县| 宁陵县| 凤庆县| 右玉县| 新乡县| 栾城县| 兰州市| 册亨县| 论坛| 仙桃市| 龙川县| 普兰店市| 岳阳县| 永寿县| 错那县| 甘洛县| 石门县| 河间市| 监利县| 普兰店市| 万源市| 荆门市| 山东| 长沙市| 什邡市| 年辖:市辖区| 威海市| 广平县| 嘉善县| 庄浪县| 迭部县| 额济纳旗| 嘉祥县| 明溪县| 香河县| 重庆市| 浦县| 固始县| 镇原县|