Last active
July 27, 2016 02:18
-
-
Save mengdd/dcbd33264ee1cf627dba4212574dd345 to your computer and use it in GitHub Desktop.
RxPermissions sample codes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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