Skip to content

Instantly share code, notes, and snippets.

@mengdd
Last active July 27, 2016 02:18
Show Gist options
  • Save mengdd/dcbd33264ee1cf627dba4212574dd345 to your computer and use it in GitHub Desktop.
Save mengdd/dcbd33264ee1cf627dba4212574dd345 to your computer and use it in GitHub Desktop.
RxPermissions sample codes
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rxPermissions = RxPermissions.getInstance(this);
rxPermissions.setLogging(true);
setContentView(R.layout.act_main);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
RxView.clicks(findViewById(R.id.enableCamera))
// Ask for permissions when button is clicked
.compose(rxPermissions.ensure(Manifest.permission.CAMERA))
.subscribe(new Action1<Boolean>() {
@Override
public void call(Boolean granted) {
Log.i(TAG, "Permission result " + granted);
if (granted) {
releaseCamera();
camera = Camera.open(0);
try {
camera.setPreviewDisplay(surfaceView.getHolder());
camera.startPreview();
} catch (IOException e) {
Log.e(TAG, "Error while trying to display the camera preview", e);
}
} else {
Toast.makeText(MainActivity.this,
"Permission denied, can't enable the camera",
Toast.LENGTH_SHORT).show();
}
}
},
new Action1<Throwable>() {
@Override
public void call(Throwable t) {
Log.e(TAG, "onError", t);
}
},
new Action0() {
@Override
public void call() {
Log.i(TAG, "OnComplete");
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment