其他
PermissionX重磅更新,支持自定义权限提醒对话框
大家早上好,今天带来一篇原创。很高兴告诉大家,PermissionX又出新版本了。
.permissions(Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION)
.request { allGranted, grantedList, deniedList ->
if (allGranted) {
Toast.makeText(activity, "所有申请的权限都已通过", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(activity, "您拒绝了如下权限:$deniedList", Toast.LENGTH_SHORT).show()
}
}
permissionList.add(Manifest.permission.ACCESS_FINE_LOCATION)
if (Build.VERSION.SDK_INT >= 29) {
permissionList.add(Manifest.permission.ACCESS_BACKGROUND_LOCATION)
}
PermissionX.init(this)
.permissions(permissionList)
.request { allGranted, grantedList, deniedList ->
if (allGranted) {
Toast.makeText(activity, "所有申请的权限都已通过", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(activity, "您拒绝了如下权限:$deniedList", Toast.LENGTH_SHORT).show()
}
}
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
PermissionX.init(this)
.permissions(Manifest.permission.ACCESS_FINE_LOCATION)
.request { allGranted, grantedList, deniedList ->
}
}
}
FragmentManager fragmentManager;
if (fragment != null) {
fragmentManager = fragment.getChildFragmentManager();
} else {
fragmentManager = activity.getSupportFragmentManager();
}
...
}
/**
* Return the instance of positive button on the dialog. Your dialog must have a positive button to proceed request.
* @return The instance of positive button on the dialog.
*/
abstract public @NonNull View getPositiveButton();
/**
* Return the instance of negative button on the dialog.
* If the permissions that you request are mandatory, your dialog can have no negative button.
* In this case, you can simply return null.
* @return The instance of positive button on the dialog, or null if your dialog has no negative button.
*/
abstract public @Nullable View getNegativeButton();
/**
* Provide permissions to request. These permissions should be the ones that shows on your rationale dialog.
* @return Permissions list to request.
*/
abstract public @NonNull List<String> getPermissionsToRequest();
}
scope.showRequestReasonDialog(myRationaleDialog)
...
<style name="CustomDialog" parent="android:style/Theme.Dialog">
<!--背景颜色及和透明程度-->
<item name="android:windowBackground">@android:color/transparent</item>
<!--是否去除标题 -->
<item name="android:windowNoTitle">true</item>
<!--是否去除边框-->
<item name="android:windowFrame">@null</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--是否模糊-->
<item name="android:backgroundDimEnabled">true</item>
</style>
</resources>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#272727" />
<corners android:radius="20dp" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#2084c2" />
<corners android:radius="20dp" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#7d7d7d" />
<corners android:radius="20dp" />
</shape>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/custom_dialog_bg"
android:orientation="vertical"
>
<TextView
android:id="@+id/messageText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#fff"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="20dp"
android:scrollbars="none"
android:layout_weight="1">
<LinearLayout
android:id="@+id/permissionsLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
/>
</ScrollView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp">
<Button
android:id="@+id/negativeBtn"
android:layout_width="120dp"
android:layout_height="46dp"
android:background="@drawable/negative_button_bg"
android:textColor="#fff"
android:text="拒绝"/>
<Button
android:id="@+id/positiveBtn"
android:layout_width="120dp"
android:layout_height="46dp"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:background="@drawable/positive_button_bg"
android:textColor="#fff"
android:text="开启"/>
</LinearLayout>
</LinearLayout>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bodyItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textSize="16sp"
android:textColor="#fff">
</TextView>
class CustomDialog(context: Context, val message: String, val permissions: List<String>) : RationaleDialog(context, R.style.CustomDialog) {
private val permissionMap = mapOf(Manifest.permission.READ_CALENDAR to Manifest.permission_group.CALENDAR,
Manifest.permission.WRITE_CALENDAR to Manifest.permission_group.CALENDAR,
Manifest.permission.READ_CALL_LOG to Manifest.permission_group.CALL_LOG,
Manifest.permission.WRITE_CALL_LOG to Manifest.permission_group.CALL_LOG,
Manifest.permission.PROCESS_OUTGOING_CALLS to Manifest.permission_group.CALL_LOG,
Manifest.permission.CAMERA to Manifest.permission_group.CAMERA,
Manifest.permission.READ_CONTACTS to Manifest.permission_group.CONTACTS,
Manifest.permission.WRITE_CONTACTS to Manifest.permission_group.CONTACTS,
Manifest.permission.GET_ACCOUNTS to Manifest.permission_group.CONTACTS,
Manifest.permission.ACCESS_FINE_LOCATION to Manifest.permission_group.LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION to Manifest.permission_group.LOCATION,
Manifest.permission.ACCESS_BACKGROUND_LOCATION to Manifest.permission_group.LOCATION,
Manifest.permission.RECORD_AUDIO to Manifest.permission_group.MICROPHONE,
Manifest.permission.READ_PHONE_STATE to Manifest.permission_group.PHONE,
Manifest.permission.READ_PHONE_NUMBERS to Manifest.permission_group.PHONE,
Manifest.permission.CALL_PHONE to Manifest.permission_group.PHONE,
Manifest.permission.ANSWER_PHONE_CALLS to Manifest.permission_group.PHONE,
Manifest.permission.ADD_VOICEMAIL to Manifest.permission_group.PHONE,
Manifest.permission.USE_SIP to Manifest.permission_group.PHONE,
Manifest.permission.ACCEPT_HANDOVER to Manifest.permission_group.PHONE,
Manifest.permission.BODY_SENSORS to Manifest.permission_group.SENSORS,
Manifest.permission.ACTIVITY_RECOGNITION to Manifest.permission_group.ACTIVITY_RECOGNITION,
Manifest.permission.SEND_SMS to Manifest.permission_group.SMS,
Manifest.permission.RECEIVE_SMS to Manifest.permission_group.SMS,
Manifest.permission.READ_SMS to Manifest.permission_group.SMS,
Manifest.permission.RECEIVE_WAP_PUSH to Manifest.permission_group.SMS,
Manifest.permission.RECEIVE_MMS to Manifest.permission_group.SMS,
Manifest.permission.READ_EXTERNAL_STORAGE to Manifest.permission_group.STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE to Manifest.permission_group.STORAGE,
Manifest.permission.ACCESS_MEDIA_LOCATION to Manifest.permission_group.STORAGE
)
private val groupSet = HashSet<String>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.custom_dialog_layout)
messageText.text = message
buildPermissionsLayout()
window?.let {
val param = it.attributes
val width = (context.resources.displayMetrics.widthPixels * 0.8).toInt()
val height = param.height
it.setLayout(width, height)
}
}
override fun getNegativeButton(): View? {
return negativeBtn
}
override fun getPositiveButton(): View {
return positiveBtn
}
override fun getPermissionsToRequest(): List<String> {
return permissions;
}
private fun buildPermissionsLayout() {
for (permission in permissions) {
val permissionGroup = permissionMap[permission]
if (permissionGroup != null && !groupSet.contains(permissionGroup)) {
val textView = LayoutInflater.from(context).inflate(R.layout.permissions_item, permissionsLayout, false) as TextView
textView.text = context.packageManager.getPermissionGroupInfo(permissionGroup, 0).loadLabel(context.packageManager)
permissionsLayout.addView(textView)
groupSet.add(permissionGroup)
}
}
}
}
.permissions(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.RECORD_AUDIO)
.onExplainRequestReason { scope, deniedList, beforeRequest ->
val message = "PermissionX需要您同意以下权限才能正常使用"
val dialog = CustomDialog(context, message, deniedList)
scope.showRequestReasonDialog(dialog)
}
.onForwardToSettings { scope, deniedList ->
val message = "您需要去设置中手动开启以下权限"
val dialog = CustomDialog(context, message, deniedList)
scope.showForwardToSettingsDialog(dialog)
}
.request { allGranted, grantedList, deniedList ->
if (allGranted) {
Toast.makeText(activity, "所有申请的权限都已通过", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(activity, "您拒绝了如下权限:$deniedList", Toast.LENGTH_SHORT).show()
}
}
PermissionX开源库地址:
https://github.com/guolindev/PermissionX
...
implementation 'com.permissionx.guolindev:permissionx:1.3.0'
}
...
implementation 'com.permissionx.guolindev:permission-support:1.3.0'
}