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

首頁 > 系統 > Android > 正文

Android權限HaloPermission詳細使用

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

1. 常規使用

請求一個權限,然后接收結果回調

HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)      .setListener(object: PermissionListener{        override fun onPermissionDenied(permissions: List<String>) {          {your code for deny}        }        override fun onPermissionGrand(permissions: List<String>) {          {your code for grand}        }      }).run()

請求多個權限

HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CALL_PHONE)      .{省略代碼}          //or        val permissions:Array<String> = arrayOf("","")    HoloPermission.with(this,*permissions)      .{省略代碼}

只關心權限被允許(未被允許)的回調

HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)      .setGrandAction(object:GrandAction{        override fun onPermissionGrand(permissions: List<String>) {          {your code for grand}        }      }).run()

2. RationaleRender使用

如果你想向用戶解釋請求權限的原因,你可以使用setRationaleRender方法

HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)            .{省略回調設置代碼}            .setRationaleRender("為了確保功能的正常使用,請允許接下來的權限請求申請。")            .run()

如果你想自定義RationaleRender的樣式,比如:

HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)          .{省略回調設置代碼}          .setRationaleRender(object:RationaleRender{            override fun show(ctx: Context, permission: List<String>, process: RationaleRender.Process) {              //自定義使用了一個`Toast`展示信息。              Toast.makeText(ctx,"為了確保功能的正常使用,請允許接下來的權限請求申請。",Toast.LENGTH_SHORT).show()              //**為了確保后續的流程繼續執行,你需要在適當的時候調用process的`onNext`或`onCancel`方法**              process.onNext()              //onNext()表示繼續后面的執行              //onCancel會取消流程的執行,并且會最終回調onPermissionDenied方法            }          })          .run()

關于此回調的觸發說明:

  1. 如果app之前請求過該權限,被用戶拒絕, 這個方法回回調。
  2. 如果用戶之前拒絕權限的時候勾選了對話框中”Don't ask again”的選項,那么這個方法不會回調
  3. 如果設備策略禁止應用擁有這條權限, 這個方法也不會回調

3. SettingRender使用

如果你想向用戶解釋請求權限的原因,你可以使用setRationaleRender方法

HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)         .{省略回調設置代碼}         .setSettingRender("無法使用外部存儲,請設置權限以便使用。")         .run()

如果你想自定義SettingRender的樣式,比如:

HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)          .{省略回調設置代碼}          .setSettingRender(object:SettingRender{            override fun show(ctx: Context, permission: List<String>, process: SettingRender.Process) {              //自定義使用了一個`Toast`展示信息。              Toast.makeText(ctx,"無法使用外部存儲,請設置權限以便使用。",Toast.LENGTH_SHORT).show()              //**為了確保后續的流程繼續執行,你需要在適當的時候調用process的`onNext`或`onCancel`方法**              process.onNext()              //onNext()表示繼續后面的執行,HaloPermission將打開系統應用權限設置界面              //onCancel會取消流程的執行,不會打開系統應用權限設置界面,最終會回調onPermissionDenied方法            }          })          .run()

如果你覺得HaloPermission打開的權限設置界面不是您所滿意的,你可以重寫SettingRender的getCustomSettingIntent方法提供一個Intent,如果返回null則將使用HaloPermission的默認方式打開:

HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)          .{省略回調設置代碼}          .setSettingRender(object:SettingRender{            override fun show(ctx: Context, permission: List<String>, process: SettingRender.Process) {              {省略的代碼}            }            //自定義SettingIntent            override fun getCustomSettingIntent(ctx: Context): Intent? {                        return super.getCustomSettingIntent(ctx)            }          })          .run()

4. 自定義權限校驗規則

兩步即可實現

   //1. 創建自定義PermissionChecker    class CustomChecker:PermissionChecker{      override fun isPermissionGranted(ctx: Context, permission: String): Boolean {        {使用你的規則}      }    }        //2. 使用自定義規則    HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)      .{省略常規代碼}      .run(CustomChecker())

除非你非常有把握,否則不建議使用自定義權限校驗規則,因為HaloPermission會盡可能的去適配和兼容

5. 自定義請求方式

HaloPermission默認使用ShadowActivity的形式請求權限,當然只要你愿意,您可以使用Fragment的形式去實現,HaloPermission本身也提供了Fragment的請求方式,但是最終去掉了這部分的實現,因為對于Fragment的使用機制,如果使用不當,可能會出現一些奇怪的問題,我想這是你我都不愿看到的。同樣的,兩步即可實現自定義請求方式

    //1. 創建自定義PermissionCaller    class CustomCaller: PermissionCaller{       override fun requestPermission(ctx: Context, responder: PermissionResponder, vararg permision: String) {         {可以仿造HaloPermission實現,最終要在適當的時候調用responder讓流程正常進行}       }    }        //2. 使用自定義規則    HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)      .{省略常規代碼}      .run(CustomCaller())

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定结县| 成安县| 黔西| 湛江市| 南城县| 临海市| 苗栗市| 嘉祥县| 龙泉市| 游戏| 荔浦县| 富顺县| 广德县| 郯城县| 绍兴市| 漳平市| 博白县| 栖霞市| 龙海市| 共和县| 寿阳县| 深泽县| 唐河县| 滕州市| 闻喜县| 新泰市| 乌鲁木齐市| 楚雄市| 万安县| 黄浦区| 平果县| 会理县| 个旧市| 德州市| 阿瓦提县| 夏邑县| 彩票| 志丹县| 盖州市| 金沙县| 星子县|