具有相机权限的导航抽屉

时间:2017-02-08 06:51:53

标签: android android-permissions

嘿,我想在启动应用程序时获得权限,它包含导航抽屉,但是当我第一次打开我的应用程序时,即使我在清单中请求了权限,它也不会显示权限弹出窗口。

代码 的 MainActivity.java 下面的课程包括导航抽屉的实现,我曾在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);
}

}

1 个答案:

答案 0 :(得分:1)

你的错误是==,它应该如下;

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