如果他/她第一次拒绝,我想在这种情况下向用户请求许可。我已设置权限,但即使按拒绝选项,我的应用也会运行。我有一个代码,应该做我想做的事情,但当我点击Alt + Enter时,我得到解决符号 Snackbar 它创建了另一个活动并保持 -make 和 -permision_available_camera 会出现红色错误。
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA) {
// BEGIN_INCLUDE(permission_result)
// Received permission result for camera permission.
Log.i(TAG, "Received response for Camera permission request.");
// Check if the only required permission has been granted
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Camera permission has been granted, preview can be displayed
Log.i(TAG, "CAMERA permission has now been granted. Showing preview.");
Snackbar.make(mLayout, R.string.permision_available_camera,
Snackbar.LENGTH_SHORT).show();
} else {
Log.i(TAG, "CAMERA permission was NOT granted.");
Snackbar.make(mLayout, R.string.permissions_not_granted,
Snackbar.LENGTH_SHORT).show();
}
答案 0 :(得分:2)
@Arjun Thakun这是一种管理再次询问权限概念的方法,并处理用户在询问权限时可以回答的所有方式
void loopOver( std::vector<Particle*> const vec ){
for (auto i = vec.begin(); i != vec.end(); i++){
std::cout << "avem " << (*i)->getXPos() << " " << (*i)->getYPos() << std::endl;
}
}
答案 1 :(得分:1)
尝试一下
基本上,这里我们试图从外部存储中读取内容。为此,我们必须询问用户运行时权限。如果用户允许,可以,但是如果用户拒绝该许可,它将显示第二个对话框/消息框,直到用户接受该许可为止
public class MainActivity extends AppCompatActivity {
public static int REQUEST_PERMISSION=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
permission_fn();
}
private void requestStoragePermission()
{
if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE))
{
new AlertDialog.Builder(this)
.setTitle("Permission Needed")
.setMessage("Permission is needed to access files from your device...")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},REQUEST_PERMISSION);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
}
else
{
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},REQUEST_PERMISSION);
}
}
private void permission_fn()
{
if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.READ_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED)
{
if(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE))
{
}
else
{
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},REQUEST_PERMISSION);
}
}
else {
requestStoragePermission();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Thanks for enabling the permission", Toast.LENGTH_SHORT).show();
//do something permission is allowed here....
} else {
Toast.makeText(this, "Please allow the Permission", Toast.LENGTH_SHORT).show();
}
}
}
}
在AndroidManifest.xml文件中添加用户权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>