目录
一、效果展示
二、代码实现
1、设置权限
2、UI
3、代码逻辑
之前写的基于原生api太复杂了,现基于PermissionX重新实现。
一、效果展示
Android基于PermissionX请求mic权限
二、代码实现
1、设置权限
在manifest文件中配置RECORD_AUDIO权限
<uses-permission android:name="android.permission.RECORD_AUDIO" />
2、添加依赖
dependencies {
implementation 'com.guolindev.permissionx:permissionx:1.7.1'
}
3、UI
简单的button按钮,点击触发动态申请权限逻辑文章来源:https://www.toymoban.com/news/detail-509651.html
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/btPermission"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请求mic权限" />
</LinearLayout>
4、代码逻辑
package com.example.myapplication;
import static android.content.ContentValues.TAG;
import android.Manifest;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import com.permissionx.guolindev.PermissionX;
import com.permissionx.guolindev.callback.ExplainReasonCallback;
import com.permissionx.guolindev.callback.ForwardToSettingsCallback;
import com.permissionx.guolindev.request.ExplainScope;
import com.permissionx.guolindev.request.ForwardScope;
import java.util.List;
/**
* Created by tzbc
*/
public class PermissionActivity extends FragmentActivity implements View.OnClickListener {
private Button permission;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kk_activity);
permission = findViewById(R.id.btPermission);
permission.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == null) {
return;
}
if (v.getId() == R.id.btPermission) {
judgeNeedRequestMicPermission();
}
}
/**
* 请求mic权限
*/
private void judgeNeedRequestMicPermission() {
Log.v(TAG, "judgeNeedRequestMicPermission...");
PermissionX.init(this).permissions(Manifest.permission.RECORD_AUDIO)
.onExplainRequestReason(new ExplainReasonCallback() {
@Override
public void onExplainReason(@NonNull ExplainScope scope, @NonNull List<String> deniedList) {
scope.showRequestReasonDialog(deniedList, "这是必要的权限",
"ok", "cancel");
}
})
.onForwardToSettings(new ForwardToSettingsCallback() {
@Override
public void onForwardToSettings(@NonNull ForwardScope scope, @NonNull List<String> deniedList) {
scope.showForwardToSettingsDialog(deniedList, "前往设置页面手动开启权限",
"ok", "cancel");
}
})
.request((allGranted, grantedList, deniedList) -> {
if (allGranted) {
Toast.makeText(PermissionActivity.this, "granted"
, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(PermissionActivity.this, "deny"
, Toast.LENGTH_SHORT).show();
}
});
}
}
文章来源地址https://www.toymoban.com/news/detail-509651.html
到了这里,关于Android请求麦克风权限+系统权限页面回调的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!