https://myandroidarchive.tistory.com/2



안드로이드 6.0으로 업그레이드 되면서 권한설정이 방식이 변경되었다.


기존에는 앱에서 요청하는 모든 권한을 허용해야 앱을 사용할 수 있다.


구글은 그러한 권한방식이 좋아하지 않았는지 바꿔버렸다.


------------------------------------------------------------------------------------------------------


public class MainActivity extends AppCompatActivity {
private Activity mainActivity = this;

private static final int REQUEST_MICROPHONE = 3;
private static final int REQUEST_EXTERNAL_STORAGE = 2;
private static final int REQUEST_CAMERA = 1;

private TextView resultText;

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

Button cameraBtn = (Button)findViewById(R.id.cameraBtn);
Button storageBtn = (Button)findViewById(R.id.storageBtn);
Button micBtn = (Button)findViewById(R.id.micBtn);

resultText = (TextView)findViewById(R.id.resultText);

cameraBtn.setOnClickListener(buttonClickListener);
storageBtn.setOnClickListener(buttonClickListener);
micBtn.setOnClickListener(buttonClickListener);
}

private View.OnClickListener buttonClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.cameraBtn:
int permissionCamera = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA);
if(permissionCamera == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(mainActivity, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA);
} else {
resultText.setText("camera permission authorized");
}
break;
case R.id.storageBtn:
int permissionReadStorage = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE);
int permissionWriteStorage = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
if(permissionReadStorage == PackageManager.PERMISSION_DENIED || permissionWriteStorage == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(mainActivity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STORAGE);
} else {
resultText.setText("read/write storage permission authorized");
}
break;
case R.id.micBtn:
int permissionAudio = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.RECORD_AUDIO);
if(permissionAudio == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(mainActivity, new String[]{Manifest.permission.RECORD_AUDIO}, REQUEST_MICROPHONE);
} else {
resultText.setText("recording audio permission authorized");
}
break;
}
}
};

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_CAMERA:
for (int i = 0; i < permissions.length; i++) {
String permission = permissions[i];
int grantResult = grantResults[i];
if (permission.equals(Manifest.permission.CAMERA)) {
if(grantResult == PackageManager.PERMISSION_GRANTED) {
resultText.setText("camera permission authorized");
} else {
resultText.setText("camera permission denied");
}
}
}
break;
case REQUEST_EXTERNAL_STORAGE:
for (int i = 0; i < permissions.length; i++) {
String permission = permissions[i];
int grantResult = grantResults[i];
if (permission.equals(Manifest.permission.READ_EXTERNAL_STORAGE)) {
if(grantResult == PackageManager.PERMISSION_GRANTED) {
resultText.setText("read/write storage permission authorized");
} else {
resultText.setText("read/write storage permission denied");
}
}
}
break;
case REQUEST_MICROPHONE:
for (int i = 0; i < permissions.length; i++) {
String permission = permissions[i];
int grantResult = grantResults[i];
if (permission.equals(Manifest.permission.RECORD_AUDIO)) {
if(grantResult == PackageManager.PERMISSION_GRANTED) {
resultText.setText("recording audio permission authorized");
} else {
resultText.setText("recording audio permission denied");
}
}
}
break;
}
}
}


---------------------------------------------------------------------------------------------------------



카메라의 권한설정 메커니즘만 설명하겠다.


---------------------------------------------------------------------------------------------------------



int permissionCamera = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA);
if(permissionCamera == PackageManager.PERMISSION_DENIED) {
ActivityCompat.requestPermissions(mainActivity, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA);
} else {
resultText.setText("camera permission authorized");
}


---------------------------------------------------------------------------------------------------------



permissionCamera를 이용하여 권한을 확인한다.

권한이 있다면 else문으로 빠져나간다. 

반면에 권한이 없다면 권한을 요청한다.



---------------------------------------------------------------------------------------------------------


switch (requestCode) {
case REQUEST_CAMERA:
for (int i = 0; i < permissions.length; i++) {
String permission = permissions[i];
int grantResult = grantResults[i];
if (permission.equals(Manifest.permission.CAMERA)) {
if(grantResult == PackageManager.PERMISSION_GRANTED) {
resultText.setText("camera permission authorized");
} else {
resultText.setText("camera permission denied");
}
}
}
break;


---------------------------------------------------------------------------------------------------------


onRequestPermissionsResult는 사용자의 권한 선택을 한 내용들이 들어있다.


여기서 Camera의 권한이 있다면 camera permission authorized가 출력되고


권한이 없다면 camera permission denied가 출력된다.


예제 코드는


android permisstion : https://github.com/pchan1401-ICIL/AndroidPermission


더 좋은 설명은 아래 블로그를 참고하자


박상권님 블로그 : http://gun0912.tistory.com/55