(targetSdkVersion
< 23)
dangerous
permissions at runtime(targetSdkVersion
>= 23)
uses-permission
elementsdangerous
permissions
checkSelfPermission()
Context
and ContextCompat
requestPermissions()
Activity
and ActivityCompat
onRequestPermissionsResult()
shouldShowRequestPermissionRationale()
Activity
and ActivityCompat
true
if...RuntimePermTutorial.zip
filecompileSdkVersion 23
buildToolsVersion "23.0.0"
targetSdkVersion 23
CAMERA
and WRITE_EXTERNAL_STORAGE
on first run, as the app is totally useless without themRECORD_AUDIO
when they click the "Record Video" button, as we will not need it before thenprivate static final String PREF_IS_FIRST_RUN="firstRun";
private SharedPreferences prefs;
onCreate()
:
prefs=PreferenceManager.getDefaultSharedPreferences(this);
private boolean isFirstRun() {
boolean result=prefs.getBoolean(PREF_IS_FIRST_RUN, true);
if (result) {
prefs.edit().putBoolean(PREF_IS_FIRST_RUN, false).apply();
}
return(result);
}
onCreate()
:
if (isFirstRun()) {
// TODO
}
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
private static final String[] PERMS_TAKE_PICTURE={
CAMERA,
WRITE_EXTERNAL_STORAGE
};
private static final int RESULT_PERMS_INITIAL=1339;
dependencies {
compile 'com.commonsware.cwac:cam2:0.2.+'
compile 'com.githang:com-phillipcalvin-iconbutton:1.0.1@aar'
compile 'com.android.support:support-v4:23.0.1'
}
if (isFirstRun()) {
ActivityCompat.requestPermissions(this, PERMS_TAKE_PICTURE,
RESULT_PERMS_INITIAL);
}
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] grantResults) {
// TODO
}
private boolean hasPermission(String perm) {
return(ContextCompat.checkSelfPermission(this, perm)==
PackageManager.PERMISSION_GRANTED);
}
private boolean canTakePicture() {
return(hasPermission(CAMERA) &&
hasPermission(WRITE_EXTERNAL_STORAGE));
}
public void takePicture(View v) {
if (canTakePicture()) {
takePictureForRealz();
}
}
private boolean shouldShowTakePictureRationale() {
return(ActivityCompat.shouldShowRequestPermissionRationale(this,
CAMERA) ||
ActivityCompat.shouldShowRequestPermissionRationale(this,
WRITE_EXTERNAL_STORAGE));
}
public void takePicture(View v) {
if (canTakePicture()) {
takePictureForRealz();
}
else if (shouldShowTakePictureRationale()) {
// TODO
}
}
@+id/breadcrust
visibility
set to gone
layout
and layout-land
private TextView breadcrust;
as fieldbreadcrust=(TextView)findViewById(R.id.breadcrust);
in onCreate()
You need to grant us
permission! Tap the Take Picture button again, and we will ask
for permission.
private static final int RESULT_PERMS_TAKE_PICTURE=1340;
requestPermissions()
prompts user for everything we ask for ...even if they granted the permission to us beforeprivate boolean canRecordVideo() {
return(canTakePicture() && hasPermission(RECORD_AUDIO));
}
public void recordVideo(View v) {
if (canRecordVideo()) {
recordVideoForRealz();
}
}
private boolean shouldShowRecordVideoRationale() {
return(shouldShowTakePictureRationale() ||
ActivityCompat.shouldShowRequestPermissionRationale(this,
RECORD_AUDIO));
}
private static final String[] PERMS_ALL={
CAMERA,
WRITE_EXTERNAL_STORAGE,
RECORD_AUDIO
};
private static final int RESULT_PERMS_RECORD_VIDEO=1341;
private static final String STATE_BREADCRUST=
"com.commonsware.android.perm.tutorial.breadcrust";
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (breadcrust.getVisibility()==View.VISIBLE) {
outState.putCharSequence(STATE_BREADCRUST,
breadcrust.getText());
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
CharSequence cs=savedInstanceState.getCharSequence(STATE_BREADCRUST);
if (cs!=null) {
breadcrust.setVisibility(View.VISIBLE);
breadcrust.setText(cs);
}
}