我正在尝试开发一款在Android中使用相机硬件的应用。编码以下行
this.mCameraManager.openCamera(cameraIds[0], new CameraDevice.StateCallback() {...}
我被要求明确检查是否有权限。当我添加权限检查时,Android添加了以下行
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != 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;
}
我重写了以下方法
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
现在,我不知道在if语句和上面提到的“onRequestPermissionsResult”中写什么。非常感谢您的帮助
答案 0 :(得分:1)
// Can define this wherever
Response.Listener<JSONObject> dataListener = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Weather weather = getJsonWeatherData(response);
setWeatherobj(weather);
}
};
// This error listener is almost always the same, anyway, right?
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("Error","can't process json result");
}
}
JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, url,
null, // Data
dataListener, // onSuccess
errorListener // onError
);
MySingleton.getInstance(getApplicationContext())
.addToRequestQue(jsonObjectRequest); // Some RequestQueue
您必须向用户显示Android权限对话框,之后您可以在onRequestPermissionsResult中捕获用户动作。
我认为你应该看看这个网站:https://developer.android.com/training/permissions/requesting.html
答案 1 :(得分:0)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_CODE_PERMISSIONS:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED)
// Open Camera
break;
}
}
答案 2 :(得分:0)
试试这个:
// The permission required by the application to work properly
protected static final String[] requiredPermissions;
private static final int PERMISSION_REQUEST = 0;
static {
List<String> perms = new ArrayList<>(Arrays.asList(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA,
Manifest.permission.RECORD_AUDIO
));
requiredPermissions = perms.toArray(new String[perms.size()]);
}
在onCreate()中调用verifyPermissions()方法:
private void verifyPermissions() {
if (!hasAllPermissions()) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
this,
requiredPermissions,
PERMISSION_REQUEST
);
}
}
private boolean hasAllPermissions() {
// Check if we have all required permissions.
for (String perm : requiredPermissions) {
if (ActivityCompat.checkSelfPermission(this, perm) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST: {
// If request is cancelled, the result arrays are empty.
if (!hasAllPermissions()) {
finish();
}
return;
}
}
}