An Android permission library developed by Kotlin language with higher extensibility and compatibility.
Kotlin語言開發(fā)的Android權限庫,具有更高的擴展性和兼容性。
寫在前面的
Android運行時權限,想必對Android開發(fā)者來說并不陌生,Github上也有不少相應的庫也足夠應付現(xiàn)在的使用了,但是HaloPermission不是在無聊的造輪子,它的職責是讓自己提供的支持更完美,更能夠擁抱變化。
其實Halo是一個系列,里面的每一個庫我都會用心,盡自己所能的去寫好,我也希望大家能給予更多的支持,共同建設,讓Android開發(fā)閃射自己的Halo.
在開發(fā)HaloPermission之前,我閱讀了很多文章,也看過很多庫的源碼,所以感謝這些偉大的無私奉獻者和開源庫作者,其中包括RxPermission,HiPermission,EasyPermission,AndPermission等。
為什么是HaloPermission
- 作者的出發(fā)點(一個對事情要求完美的處女座特點)
- 基于Kotlin(雙刃劍,仁者見仁,智者見智)
- 更多的擴展性(后面會寫文章專門介紹HoloPermission的設計)
- 更多的兼容性(盡量兼容);
- 更靈活的功能配置
使用介紹
1. 常規(guī)使用
- 請求一個權限,然后接收結果回調(diào)
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)
.{省略代碼}
- 只關心權限被允許(未被允許)的回調(diào)
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)
.{省略回調(diào)設置代碼}
.setRationaleRender("為了確保功能的正常使用,請允許接下來的權限請求申請。")
.run()
如果你想自定義RationaleRender的樣式,比如:
HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
.{省略回調(diào)設置代碼}
.setRationaleRender(object:RationaleRender{
override fun show(ctx: Context, permission: List<String>, process: RationaleRender.Process) {
//自定義使用了一個`Toast`展示信息。
Toast.makeText(ctx,"為了確保功能的正常使用,請允許接下來的權限請求申請。",Toast.LENGTH_SHORT).show()
//**為了確保后續(xù)的流程繼續(xù)執(zhí)行,你需要在適當?shù)臅r候調(diào)用process的`onNext`或`onCancel`方法**
process.onNext()
//onNext()表示繼續(xù)后面的執(zhí)行
//onCancel會取消流程的執(zhí)行,并且會最終回調(diào)onPermissionDenied方法
}
})
.run()
3. SettingRender使用
對于無法獲取權限時,如果你想引導用戶打開權限設置界面,你可以使用setSettingRender方法
HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
.{省略回調(diào)設置代碼}
.setSettingRender("無法使用外部存儲,請設置權限以便使用。")
.run()
如果你想自定義SettingRender的樣式,比如:
HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
.{省略回調(diào)設置代碼}
.setSettingRender(object:SettingRender{
override fun show(ctx: Context, permission: List<String>, process: SettingRender.Process) {
//自定義使用了一個`Toast`展示信息。
Toast.makeText(ctx,"無法使用外部存儲,請設置權限以便使用。",Toast.LENGTH_SHORT).show()
//**為了確保后續(xù)的流程繼續(xù)執(zhí)行,你需要在適當?shù)臅r候調(diào)用process的`onNext`或`onCancel`方法**
process.onNext()
//onNext()表示繼續(xù)后面的執(zhí)行,HaloPermission將打開系統(tǒng)應用權限設置界面
//onCancel會取消流程的執(zhí)行,不會打開系統(tǒng)應用權限設置界面,最終會回調(diào)onPermissionDenied方法
}
})
.run()
如果你覺得HaloPermission打開的權限設置界面不是您所滿意的,你可以重寫SettingRender的getCustomSettingIntent方法提供一個Intent,如果返回null則將使用HaloPermission的默認方式打開:
HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
.{省略回調(diào)設置代碼}
.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. 自定義權限校驗規(guī)則
兩步即可實現(xiàn)
//1. 創(chuàng)建自定義PermissionChecker
class CustomChecker:PermissionChecker{
override fun isPermissionGranted(ctx: Context, permission: String): Boolean {
{使用你的規(guī)則}
}
}
//2. 使用自定義規(guī)則
HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
.{省略常規(guī)代碼}
.run(CustomChecker())
除非你非常有把握,否則不建議使用自定義權限校驗規(guī)則,因為HaloPermission會盡可能的去適配和兼容
5. 自定義請求方式
HaloPermission默認使用ShadowActivity的形式請求權限,當然只要你愿意,您可以使用Fragment的形式去實現(xiàn),HaloPermission本身也提供了
Fragment的請求方式,但是最終去掉了這部分的實現(xiàn),因為對于Fragment的使用機制,如果使用不當,可能會出現(xiàn)一些奇怪的問題,我想這是你我都不愿看到的。
同樣的,兩步即可實現(xiàn)自定義請求方式
//1. 創(chuàng)建自定義PermissionCaller
class CustomCaller: PermissionCaller{
override fun requestPermission(ctx: Context, responder: PermissionResponder, vararg permision: String) {
{可以仿造HaloPermission實現(xiàn),最終要在適當?shù)臅r候調(diào)用responder讓流程正常進行}
}
}
//2. 使用自定義規(guī)則
HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
.{省略常規(guī)代碼}
.run(CustomCaller())
實際運行效果截圖
熟話說無圖無真相,由于常規(guī)請求的效果圖比較單調(diào),這里只貼了設置了RationaleRender和SettingRender的效果截圖:
-
包含SettingRender的效果
包含RationaleRender的效果

