gpt4 book ai didi

android - 具有相机权限的抽屉导航

转载 作者:行者123 更新时间:2023-11-30 00:43:56 27 4
gpt4 key购买 nike

嘿,当我想启动包含抽屉导航的应用程序时,我正在尝试获得许可,但是当我第一次打开我的应用程序时,即使我已经在 list 中请求许可,它也没有显示许可弹出窗口。

代码MainActivity.java下面的类(class)包括抽屉导航的实现,我曾在 onCreate 中请求 checkSelfPermission,但没有任何效果。

public class MainActivity extends AppCompatActivity {

private DrawerLayout mDrawer;
private Toolbar toolbar;
private NavigationView nvDrawer;
private ActionBarDrawerToggle drawerToggle;

private static final int REQUEST_CAMERA = 0;

private View mLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);


if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA) ==
PackageManager.PERMISSION_GRANTED) {

if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.

} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA);
}
}

nvDrawer = (NavigationView) findViewById(R.id.nvView);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
setupDrawerContent(nvDrawer);
drawerToggle = setupDrawerToggle();
mDrawer.addDrawerListener(drawerToggle);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA){
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
//granted
}else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA);
}
}
}

private ActionBarDrawerToggle setupDrawerToggle() {
return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close);
}

private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}

private void selectDrawerItem(MenuItem menuItem) {
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}

最佳答案

你的错误是==,应该是这样的;

if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED)

关于android - 具有相机权限的抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42106512/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com