发布:2023/2/21 14:47:10作者:管理员 来源:本站 浏览次数:511
目录
一、效果展示
二、代码实现
1、设置权限
2、UI
3、代码逻辑
一、效果展示
系统权限弹框
不再提示后的解释弹框
跳转到应用系统权限页面
二、代码实现
1、设置权限
在manifest文件中配置RECORD_AUDIO权限
<uses-permission android:name="android.permission.RECORD_AUDIO" />
2、UI
简单的button按钮,点击触发动态申请权限逻辑
<?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>
3、代码逻辑
package com.example.myapplication.activity;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
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.appcompat.app.AppCompatActivity;
import com.example.myapplication.R;
import com.example.myapplication.utils.PermissionUtil;
import kr.co.namee.permissiongen.PermissionGen;
/**
* Created by tzbc
*/
public class AlarmActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "AlarmActivity";
private static final int REQUEST_CODE_MIC_PERMISSION = 100;
private static final int REQUEST_CODE_MIC_SETTING_PERMISSION = 101;
private Button permission;
private Dialog micDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm_activity);
permission = findViewById(R.id.btPermission);
permission.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == null) {
return;
}
switch (v.getId()) {
case R.id.btPermission:
judgeNeedRequestMicPermission();
break;
default:
break;
}
}
/**
* 请求mic权限
*/
private void judgeNeedRequestMicPermission() {
Log.v(TAG, "judgeNeedRequestMicPermission...");
boolean hasAudioPermission = PermissionUtil.judgeHasAudioPermission(this);
if (!hasAudioPermission) {
PermissionUtil.requestAudioPermission(this, REQUEST_CODE_MIC_PERMISSION);
} else {
Toast.makeText(this, "已经授予麦克风权限", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
if (REQUEST_CODE_MIC_PERMISSION == requestCode) {
//用户授予权限
if (PERMISSION_GRANTED == grantResults[0]) {
showPermissionOnToast();
} else {
//权限被拒绝
boolean showRequestPermissionRationale = PermissionUtil.shouldShowRequestPermissionRationale(this);
if (!showRequestPermissionRationale) {
//向用户解释权限使用
showMicPermissionDialog();
} else {
showPermissionOffToast();
}
}
}
}
private void showMicPermissionDialog() {
if (micDialog == null) {
micDialog = new AlertDialog.Builder(this).setTitle("test")
.setMessage("需要使用您的麦克风权限")
.setPositiveButton("Yes", (dialog, which) -> {
//跳转至系统页面
gotoAudioSettingIntent();
micDialog.dismiss();
})
.setNegativeButton("No", (dialog, which) -> {
showPermissionOffToast();
micDialog.dismiss();
})
.show();
} else {
micDialog.show();
}
micDialog.setCancelable(false);
}
private void gotoAudioSettingIntent() {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.fromParts("package", getPackageName(), null));
startActivityForResult(intent, REQUEST_CODE_MIC_SETTING_PERMISSION);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (REQUEST_CODE_MIC_SETTING_PERMISSION == requestCode) {
Log.v(TAG, "onActivityResult REQUEST_CODE_MIC_SETTING_PERMISSION");
dismissMicDialog();
//从系统页面返回再次获取当前mic权限是否授予
boolean hasAudioPermission = PermissionUtil.judgeHasAudioPermission(this);
Log.v(TAG, "onActivityResult hasAudioPermission: " + hasAudioPermission);
if (hasAudioPermission) {
showPermissionOnToast();
} else {
showPermissionOffToast();
}
}
}
private void dismissMicDialog() {
if (micDialog != null) {
micDialog.dismiss();
micDialog = null;
}
}
private void showPermissionOnToast() {
Toast.makeText(this, "麦克风权限开启成功", Toast.LENGTH_SHORT).show();
}
private void showPermissionOffToast() {
Toast.makeText(this, "麦克风权限开启失败", Toast.LENGTH_SHORT).show();
}
}
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4