栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

android请求调用操作的运行时权限

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android请求调用操作的运行时权限

在您的

onClick()
方法中尝试此代码

if(isPermissionGranted()){     call_action();}

现在,调用创建一个单独的方法:

public void call_action(){    String phnum = etPhoneno.getText().toString();    Intent callIntent = new Intent(Intent.ACTION_CALL);    callIntent.setData(Uri.parse("tel:" + phnum));    startActivity(callIntent);   }

添加以下两种用于运行时权限检查的方法:

 public  boolean isPermissionGranted() {    if (Build.VERSION.SDK_INT >= 23) {        if (checkSelfPermission(android.Manifest.permission.CALL_PHONE)     == PackageManager.PERMISSION_GRANTED) { Log.v("TAG","Permission is granted"); return true;        } else { Log.v("TAG","Permission is revoked"); ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 1); return false;        }    }    else { //permission is automatically granted on sdk<23 upon installation        Log.v("TAG","Permission is granted");        return true;    }} @Overridepublic void onRequestPermissionsResult(int requestCode,      String permissions[], int[] grantResults) {    switch (requestCode) {        case 1: { if (grantResults.length > 0         && grantResults[0] == PackageManager.PERMISSION_GRANTED) {     Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();     call_action(); } else {     Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show(); } return;        }        // other 'case' lines to check for other        // permissions this app might request    } }

还要确保将其添加到清单中:

<uses-permission android:name="android.permission.CALL_PHONE" />

碎片

如果您在中尝试使用此代码

fragment
,请更改

checkSelfPermission()

ActivityCompat.checkSelfPermission()

并且也改变

ActivityCompat.requestPermissions()

requestPermissions()



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/431054.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号