附上我参考的大神链接https://blog.csdn.net/wenzhi20102321/article/details/80487975
申请三个权限,录音和文件读写是附加的,最重要的是拨打电话权限
//1、首先将需要的权限写到manifest.xml
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
activity.java
public class MainActivity extends AppCompatActivity {
Context mContext;
private TextView bohao;
//申请三个权限,录音和文件读写是附加的,最重要的是拨打电话权限
//1、首先声明一个数组permissions,将需要的权限都放在里面
String[] permissions = new String[]{Manifest.permission.RECORD_AUDIO,
Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.CALL_PHONE};
//2、创建一个mPermissionList,逐个判断哪些权限未授予,未授予的权限存储到mPerrrmissionList中
List<String> mPermissionList = new ArrayList<>();
private final int mRequestCode = 100;//权限请求码
public final int REQUEST_CALL_PERMISSION = 10111; //拨号请求码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bohao = findViewById(R.id.bohao);
bohao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mobile = "123456";
String phone;
//这里我直接在页面创建的时候请求权限,其实不太好,这里只是为了演示
//一般是在触发某个事件的时候再请求动态权限,比如点击按钮跳转到一个拍照页面,如果权限通过就跳转,否者吐司说没有权限!
if (Build.VERSION.SDK_INT >= 23) {//6.0才用动态权限
initPermission();
}
}
});
}
//权限判断和申请
private void initPermission() {
mPermissionList.clear();//清空没有通过的权限
//逐个判断你要的权限是否已经通过
for (int i = 0; i < permissions.length; i++) {
if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(permissions[i]);//添加还未授予的权限
}
}
//申请权限
if (mPermissionList.size() > 0) {//有权限没有通过,需要申请
ActivityCompat.requestPermissions(this, permissions, mRequestCode);
}else{
//说明权限都已经通过,可以做你想做的事情去,例如拨打电话123
call("tel:"+"123");
}
}
/**
* 检查权限后的回调
* @param requestCode 请求码
* @param permissions 权限
* @param grantResults 结果
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
boolean hasPermissionDismiss = false;//有权限没有通过
if (mRequestCode == requestCode) {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] == -1) {
hasPermissionDismiss = true;
}
}
//如果有权限没有被允许
if (hasPermissionDismiss) {
showPermissionDialog();//跳转到系统设置权限页面,或者直接关闭页面,不让他继续访问
}else{
//全部权限通过,可以进行下一步操作。。。
}
}
}
/**
* 不再提示权限时的展示对话框
*/
AlertDialog mPermissionDialog;
String mPackName = "com.huawei.liwenzhi.weixinasr";
private void showPermissionDialog() {
if (mPermissionDialog == mPermissionDialog ) {
mPermissionDialog = new AlertDialog.Builder(this)
.setMessage("已禁用权限,请手动授予")
.setPositiveButton("设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
goIntentSetting();//跳转到设置
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//关闭页面或者做其他操作
cancelPermissionDialog();
}
})
.create();
}
mPermissionDialog.show();
}
//跳转到设置页面
private void goIntentSetting() {
Uri packageURI = Uri.parse("package:" + "com.example.administrator.hello");
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,packageURI);
startActivity(intent);
try {
mContext.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
//关闭对话框
private void cancelPermissionDialog() {
mPermissionDialog.cancel();
}
/**
* 拨打电话(直接拨打)
* @param telPhone 电话
*/
public void call(String telPhone){
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse(telPhone));
startActivity(intent);
}
}
网友评论