Android 权限请求代码问题
Posted
技术标签:
【中文标题】Android 权限请求代码问题【英文标题】:Android Permission Request Code Issue 【发布时间】:2016-09-12 05:05:33 【问题描述】:如何申请权限?我尝试记录文档,但常量 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?
【问题讨论】:
也许这对你有用***.com/a/36787464/3436179 查看此参考:tutorialspoint.com/android/android_phone_calls.htm 【参考方案1】: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"); makeCall("100"); Link for more details
【讨论】:
【参考方案2】:对于较低版本,您只需在清单中声明权限, 但是对于 marshmellow,您需要在代码中给出它,您要在其中执行代码。
在这里,您要拨打电话。因此,在编写调用的代码块中插入/包含下面提供的代码。
public void makeCall()
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "123456"));
int result = ContextCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE);
if (result == PackageManager.PERMISSION_GRANTED)
startActivity(intent);
else
requestForCallPermission();
private void requestPermission()
if(ActivityCompat.shouldShowRequestPermissionRationale(activity,Manifest.permission.CALL_PHONE))
else
ActivityCompat.requestPermissions(activity,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();
break;
【讨论】:
是的,我明白,但是如何删除PERMISSION_REQUEST_CODE 对不起,我忘了告诉你,你需要像下面这样在顶部声明它 private static final int PERMISSION_REQUEST_CODE = 1; 并且还在其他部分通过 CALL_PHONE 而不是 ACCESS_FINE_LOCATION 更改 requestPermission() 方法中的权限【参考方案3】: if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "123456"));
startActivity(intent);
尝试这样做。
【讨论】:
谢谢它开始工作我想我把 startActivity 放在 if 之前:) 是的,这是唯一的问题。保持学习。 :)【参考方案4】:试试下面的代码,希望对你有帮助。 首先,这会在允许它呼叫号码后询问您是否允许弹出窗口。
if (ContextCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
if (ActivityCompat.shouldShowRequestPermissionRationale(HomePanelActivity.this,
Manifest.permission.CALL_PHONE))
ActivityCompat.requestPermissions(HomePanelActivity.this, new String[]Manifest.permission.CALL_PHONE, REQUEST_PERMISSION);
else
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumber));
if (ActivityCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED)
startActivity(callIntent);
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
switch (requestCode)
case 10:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumberToCall));
if (ActivityCompat.checkSelfPermission(HomePanelActivity.this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED)
startActivity(callIntent);
else
Snackbar.make(drawerLayout, "You Deny permission", Snackbar.LENGTH_SHORT).show();
return;
;
【讨论】:
我在启动时需要 Permisson 并在按钮单击时进行调用。而且我需要许可,所以拒绝是没有选择的...... 这需要 API23 我的应用是 minSDK 15 是的,您需要将 SDK 版本编译为 23。至少 15 可以工作。【参考方案5】:您还需要在 AndroidManifest.xml 中指定要使用的权限 喜欢
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
【讨论】:
以上是关于Android 权限请求代码问题的主要内容,如果未能解决你的问题,请参考以下文章