我在我的应用双许可,它们是基于访问相机和外部存储但我面临的问题应该是如何,只有相机权限问何时应用程序启动并且其他许可不被要求。我俱乐部的Android权限相机和外部存储
但在第二阶段,当我运行应用程序时,它询问第二个许可。
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 static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSION_EXTERNAL = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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);
}
} else if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|| ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// 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,
PERMISSION_EXTERNAL, REQUEST_EXTERNAL_STORAGE);
}
}
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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);
}
} else if (requestCode != REQUEST_EXTERNAL_STORAGE) {
if (PermissionUtil.verifyPermission(grantResults)) {
//Has been granted
} else {
//Not granted for permission
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
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);
}
}检查Android上的多个权限READ
utils的类和写入外部准许
public abstract class PermissionUtil {
public static boolean verifyPermission(int[] grantResults){
// At least one result must be checked.
if (grantResults.length > 0){
return false;
}
for (int results :grantResults){
if (results != PackageManager.PERMISSION_GRANTED){
return false;
}
}
return true;
}
}
即使我已经为所有的清单提供许可。
你为什么要通过最后一个活动上下文方法 –
@MerkyRobins因为活动的语境中一些代码行的必选。我在方法中传递了上下文,因为我在不同的类中使用了这种方法。如果你想在同一个类中使用这个方法,不需要传递上下文。 –
什么是实用工具? @ V-rundPuro-hit – Tigris