public void checkPermissions() {
Log.i(TAG, " Checking permissions.");
// Verify that all required contact permissions have been granted.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// Contacts permissions have not been granted.
Log.i(TAG, "Call Phone permissions has NOT been granted. Requesting permissions.");
requestPermissions();
} else {
// Contact permissions have been granted. Show the contacts fragment.
Log.i(TAG,
"Contact permissions have already been granted");
showContactDetails();
}
}
public void customDialog(Context ctx) {
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(ctx, android.R.style.Theme_Material_Light_Dialog_Alert);
builder.setMessage("Call permissions are needed to demonstrate access").setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
ActivityCompat.requestPermissions(CallChoosyActivity.this, Permissions, REQUEST_CALL_PHONE);
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
} else {
builder = new AlertDialog.Builder(ctx);
builder.setMessage("Call permissions are needed to demonstrate access").setNeutralButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
public void requestPermissions() {
// BEGIN_INCLUDE(contacts_permission_request)
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
// Provide an additional rationale to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
// For example, if the request has been denied previously.
Log.i(TAG, "Displaying call permission rationale to provide additional context.");
customDialog(CallChoosyActivity.this);
} else {
// Contact permissions have not been granted yet. Request them directly.
ActivityCompat.requestPermissions(this, Permissions, REQUEST_CALL_PHONE);
}
// END_INCLUDE(contacts_permission_request)
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_CALL_PHONE) {
Log.i(TAG, "Received response for contact permissions request.");
// We have requested multiple permissions for contacts, so all of them need to be
// checked.
if (PermissionUtil.verifyPermissions(grantResults)) {
// All required permissions have been granted, display contacts fragment.
showContactDetails();
} else {
Log.i(TAG, "Contacts permissions were NOT granted.");
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
public void showContactDetails() {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + PartyTabsActivity.numbersList.get(0)));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(callIntent);
}
当我运行app时,它在logcat中显示这个
java.lang.SecurityException:权限拒绝:从ProcessRecord{2dd511f 24656:com.marg.pharmanxt/u0a158}(PID=24656,UID=10158)中启动意图{act=android.Intent.action.call dat=tel:xxxxxxxxxcapt=2dd511f 24656:com.marg.pharmanxt/u0a158}(PID=24656,UID=10158)并吊销了Android.Permission.call_phone在Android.os.parcel.readexception(parcel.java:1599)
根据此更改您的权限请求代码。
int checkPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
if (checkPermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.CALL_PHONE},
REQUEST_CALL_PHONE);
} else {
customDialog(CallChoosyActivity.this);
}
保持OnRequestPermissionsResult
不变
问题内容: 我在Android 6.0(棉花糖)上遇到了日期格式问题。引发以下异常的代码是我的应用程序用于API请求(“客户端”)的纯Java库(单独构建)。如果相关的话,该库是用Java 1.6构建的……无论如何,这是代码。 … 具有价值; …“修剪”之后是; 该代码自Froyo成立以来一直有效,并且已经过单元测试。除了棉花糖,所有东西都会抛出异常。 偏移量“ 21”是10:59中“ 9”之后的
我有一个关于新的Android棉花糖发布的问题: 是否可以通过意图或类似的方式显示特定应用程序的权限屏幕? 有可能用下面的代码显示app设置--有直接打开权限屏幕的模拟解决方案吗? 我已经做了一些研究,但我没有找到一个适当的解决办法-我将感谢每一个帮助!
问题内容: 我正在创建一个可产生现场乐器声音的应用程序,并且计划使用Android棉花糖(6.0版)中的新Midi API。我已经在http://developer.android.com/reference/android/media/midi/package- summary.html上 阅读了软件包概述文档,并且我知道如何生成Midi笔记,但是我仍然不确定:我该怎么办在生成它们的Midi数据
问题内容: 我有一个图像URI,我想将此URI转换为真实路径。我看了很多答案,但没有一个对我有用。我正在使用棉花糖6.0.1。图片URI为。 码: 问题答案: A 不是。A 不必代表您可以访问的文件系统上的文件。将可能指向的内容是: 存储在无法访问的可移动存储中 存储在另一个应用程序的内部存储中,您无法访问 以加密形式存储,需要解密时 存储在SQLite数据库的BLOB列中,需要将其加载并提供服务
问题内容: 我要尝试测试其呼叫顺序的三个功能。 假设在模块module.py中,我有以下内容 我想检查b在a之后和c之前被调用。因此,对a,b和c中的每一个进行模拟都是容易的: 检查每个单独的模拟都被调用也很容易。如何检查呼叫相对于彼此的顺序? 将无法使用,因为每个模拟都单独维护。 我尝试使用副作用来记录每个调用: 但这仅给我提供了与模拟一起调用的args,而没有给实际调用的模拟。我可以添加更多逻
IntelliJ Android Studio上次更新(2016年1月30日起)支持Java8吗? 我用了一个lambda表达式 注意:我使用的是最后一个API级别