Android:如何在onCreate之前首先要求2个权限?

时间:2017-08-09 02:03:53

标签: android permissions

我的权限有问题.. 在用户允许我的应用获取权限请求后,如何更新onCreate或其他内容?

例如:

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

        // checking if it's not allowed yet

        if(!hasPermissions(this, PERMISSIONS)){
            ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
        }


       // now here how can i do something or start New Activity after he allows all permissions?



}

如果我这样做:

else {

}
除非我重启我的应用

,否则

对我无效

1 个答案:

答案 0 :(得分:0)

你应该使用libs https://github.com/hotchemi/PermissionsDispatcher

示例:

 @RuntimePermissions
 public class AppTopView1Activity extends AppCompatActivity{
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_top_view_1);
    AppTopView1ActivityPermissionsDispatcher.checkPermissionWithCheck(this);
  }
 }

@NeedsPermission({Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA})
void checkPermission(){
    // allow Permission : Code here
}

@OnPermissionDenied({Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA})
void showDeniedForCamera() {
    // don't allow Permission : Code here
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    AppTopView1ActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}