gpt4 book ai didi

Android权限请求代码问题

转载 作者:行者123 更新时间:2023-11-29 14:24:07 25 4
gpt4 key购买 nike

如何申请权限?我尝试了文档,但常量 int 请求代码 MY_PERMISSIONS_REQUEST_CALL_PHONE 似乎不起作用,还有什么要记住的向后兼容性吗?

ActivityCompat.requestPermissions(getApplicationContext(),
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_CALL_PHONE);

如何声明 MY_PERMISSIONS_REQUEST_CALL_PHONE 常量 int?

最佳答案

public void makeCall(String s)
{
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + s));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){

requestForCallPermission();

} else {
startActivity(intent);

}
}
public void requestForCallPermission()
{

if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CALL_PHONE))
{
}
else {

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CALL_PHONE},PERMISSION_REQUEST_CODE);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
{
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
makeCall("100");
}
break;
}
}

//现在调用方法 makeCall("your_desire_phone_numder");打电话(“100”); Link for more details

关于Android权限请求代码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37253397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com