我正在我的应用程序中实现运行时权限,但我无法显示CALL_PHONE的权限。我需要显示两个权限,read_contact和call_phone。我可以完美地显示read_contact但不能显示call_phone权限。
我正在使用AVD模拟器进行测试。
一些帮助将是apreciate!
这是我的代码:
callPesmission.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestCallPhonePermission();
}
});
contactPesmission.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
requestContactsPermission();
}
});
private void requestContactsPermission() {
String locationPermission = Manifest.permission.READ_CONTACTS;
int hasPermission = ContextCompat.checkSelfPermission(getActivity(), locationPermission);
String[] permissions = new String[] { locationPermission };
if (hasPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(permissions, REQUEST_CONTACTS);
} else {
Toast.makeText(getActivity(), "We already have persmission", Toast.LENGTH_SHORT).show();
}
}
private void requestCallPhonePermission() {
String callPermission = Manifest.permission.CALL_PHONE;
int hasPermission = ContextCompat.checkSelfPermission(getActivity(), callPermission);
String[] permissions = new String[] { callPermission };
if (hasPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(permissions, REQUEST_CALL_PHONE);
} else {
Toast.makeText(getActivity(), "We already have persmission", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CONTACTS:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getActivity(), "READ_CONTACTS GRANTED", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "READ_CONTACTS DENIED", Toast.LENGTH_SHORT).show();
}
break;
case REQUEST_CALL_PHONE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getActivity(), "CALL_PHONE GRANTED", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "CALL_PHONE DENIED", Toast.LENGTH_SHORT).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
答案 0 :(得分:4)
我认为你的代码没有任何问题,
确认它是否确实存在于manifest.xml中。
Dialog不会显示它是否存在于清单中,如果我们尝试使用该功能,则可能会抛出安全异常
答案 1 :(得分:3)
我忘了在清单中添加权限.....
<uses-permission android:name="android.permission.CALL_PHONE" />
答案 2 :(得分:0)
添加此
private static final int PHONE_CALL_REQUEST=0;
和您的按钮
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.CALL_PHONE},PHONE_CALL_REQUEST);
}
else
{
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "+01723915447"));
startActivity(intent);
}
答案 3 :(得分:0)
步骤1:
<uses-permission android:name="android.permission.CALL_PHONE" />
第2步:
<permission
android:name="mypackage.permission.CALL_PHONE"
android:protectionLevel="signature" />
NB::步骤2中的 mypackage 是您的根项目软件包名称,例如com.myapp,因此步骤2中的上述内容将变为
<permission
android:name="com.myapp.permission.CALL_PHONE"
android:protectionLevel="signature" />
最后一步:
在您的Java / Kotin函数中,Manifest.permission.CALL_PHONE
将可用
答案 4 :(得分:-1)
在奥利奥使用,
android.Manifest.permission.CALL_PHONE
您忘记了android.
部分。