//动态权限 implementation('com.github.hotchemi:permissionsdispatcher:3.1.0') kapt 'com.github.hotchemi:permissionsdispatcher-processor:3.1.0'
github:https://github.com/permissions-dispatcher/PermissionsDispatcher
getContactsWithPermissionCheck()
@NeedsPermission( Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.GET_ACCOUNTS ) fun getContacts() { setContentView(R.layout.activity_address_book) }
@OnNeverAskAgain( Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.GET_ACCOUNTS ) fun noPermission() { setContentView(R.layout.activity_address_book_no_content)//设置无内容layout布局 showPopwindow()//手动设置权限,popwindwo弹出框 }
@SuppressLint("all") override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) onRequestPermissionsResult(requestCode, grantResults) }
/**
* 显示手动设置权限对话框
*/
fun showPopwindow() {
//弹出popwindow对话框
var viewLayout = LayoutInflater.from(mContext).inflate(R.layout.popwindow_setting, null)
val popwindowUtils = PopwindowUtils(this)
popwindowUtils.showPop(viewLayout, viewLayout)
//弹出框的确定跟取消点击事件
MyClickUtils.OnClickView(object : MyClickUtils.Companion.MyOnClickLenerlist {
override fun onClick(view: View) {
when (view) {
viewLayout.tv_setting_confirm -> {//确定按钮
//跳转到当前应用的权限设置界面
startSystemIntent(mContext,Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
}
}
popwindowUtils.dismiss()//取消
}
}, viewLayout.tv_setting_confirm, viewLayout.tv_setting_cancle)
}
//跳转到当前应用的权限设置界面
fun startSystemIntent(mContext: Context,action:String){
var uri = Uri.fromParts("package", mContext.getPackageName(), null);
var intent = Intent()
intent.setAction(action);
intent.setData(uri);
mContext.startActivity(intent)
}
弹出框代码:
import android.app.Activity import android.view.Gravity import android.view.View import android.view.ViewGroup import android.view.WindowManager import android.widget.PopupWindow import com.cr.ucircle.R /** * 界面的下方弹出 * @property mContext Activity? * @constructor */ class PopwindowUtils(mContext: Activity) { var mContext: Activity? = null var popupWindow:PopupWindow?=null init { this.mContext = mContext } /** * view:位置 * v:popwindow的布局 */ fun showPop(view: View,v:View) { popupWindow = PopupWindow(v, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) popupWindow!!.setTouchable(true); popupWindow!!.setFocusable(true); popupWindow!!.setAnimationStyle(R.style.popwin_anim_style); popupWindow!!.setOutsideTouchable(true); // popupWindow.showAsDropDown(view, 0, 0, Gravity.BOTTOM); popupWindow!!.showAtLocation(view, Gravity.BOTTOM, 0, 0) //设置遮罩层 backgroundAlpha(0.5f) popupWindow!!.setOnDismissListener(object : PopupWindow.OnDismissListener { override fun onDismiss() { backgroundAlpha(1f) } }) } fun dismiss(){ popupWindow?.dismiss() } private fun backgroundAlpha(f: Float) { val attributes = mContext?.window?.attributes attributes?.alpha = f mContext?.window?.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) mContext?.window?.attributes = attributes } }