这是我的片段类
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.cameraPerm:
manageCameraPerm();
if ((ActivityCompat.checkSelfPermission(getContext(),
android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED)) {
v.startAnimation(animSlide);
allowCamera.setVisibility(View.GONE);
w = 1;
}
break;
}
}
//这是我的manageCameraPerm方法
private void manageCameraPerm() {
if (ActivityCompat.checkSelfPermission(getContext(),
android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
MY_PERMS_REQUEST_CAMERA);
} else {
Toast.makeText(getActivity(), "Camera is enabled", Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
这是因为检查权限是否被授予的if语句恰好在弹出窗口出现之后(当然还没有授予权限)。您需要在onPermissionResult中触发动画(请参阅此处的“处理权限请求响应”部分:https://developer.android.com/training/permissions/requesting.html)
答案 1 :(得分:0)
假设您正在使用Marshmallow或更高版本的设备,您必须通过回调方法onRequestPermissionsResult()处理UI代码(在此情况下设置动画然后隐藏按钮),负责在提出请求后立即检查是否授予权限:
var QuestionSchema = Schema({
id : ObjectId,
title : String,
employees : [{ type: ObjectId, ref: 'User'}]
});
var UserSchema = Schema({
username : String,
//response : String,
questions : [{ type: ObjectId, ref: 'Question'}]
});
//new schema/collection I've had to create
var AnswerSchema = Schema({
response : {type :String, default:null},
question : { type: ObjectId, ref: 'Question'},
employees : [{ type: ObjectId, ref: 'User'}],
})
module.exports = mongoose.model('Question', QuestionSchema);
module.exports = mongoose.model('User', UserSchema);
module.exports = mongoose.model('Answer', AnswersSchema);
...以及您的manageCameraPerm()方法:
Question.findOne({ title: 'Should we buy a coffee machine?'}).exec(function(err, question) {
//example data
var user = new User([{
"username": "lindelof",
"response": "yes",
},{
"username": "bailly",
"response": "no",
},{
"username": "suzan",
"response": "yes",
}]);
question.employees = [user1._id];
user.questions = [question._id];
question.save(function(err) {
if (err) throw err;
console.log(question);
user1.save(function(err) {
if (err) throw err;
});
});
});
console.log('entry saved >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
}
答案 2 :(得分:0)
在android中使用premission并不是那么简单。有一些库可以让它更容易:https://github.com/hotchemi/PermissionsDispatcher 只需使用这个库!