Din*_*uka 5 java android android-intent android-permissions
当用户点击具有数字的TextView时,我试图基本上拨号:
number_title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+"+user.getTelephone()));
activity.startActivity(callIntent);
//the above line returns the warning given below
}
});
Run Code Online (Sandbox Code Playgroud)
我得到的警告:
呼叫需要用户可能拒绝的许可.用于明确检查权限是否可用的代码
我知道如果以前没有授予权限,应用程序需要请求权限.我的问题是,如何在拨打电话前明确请求许可?
嗯,这是关于运行时权限的,因此仅在清单中声明它们是行不通的。相反,您必须在开始通话之前检查权限。
像这样的东西应该有效 - 只有当用户之前没有授予该权限(或者如果他撤销了该权限)时,才会要求用户授予权限:
private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1234;
public void yourfunction() {
number_title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.CALL_PHONE},
MY_PERMISSIONS_REQUEST_CALL_PHONE);
} else {
executeCall();
}
}
});
}
private void executeCall() {
// start your call here
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:+"+user.getTelephone()));
activity.startActivity(callIntent);
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay!
executeCall();
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我主要从谷歌获取上面的代码: http: //developer.android.com/training/permissions/requesting.html 如果您想了解更多信息,请访问链接。
编辑:
onRequestPermissionsResult将是您的活动中的回调函数。您可能需要在那里处理电话。
| 归档时间: |
|
| 查看次数: |
3533 次 |
| 最近记录: |