public class ManifestCheck extends AppCompatActivity {
private Activity calledActivity;
public ManifestCheck(Activity activity) {
calledActivity = activity;
//WebviewMain.activiet 값을 받아옴.
}
@TargetApi(Build.VERSION_CODES.M)
public void checkVerify() {
//if문에 사용할 요소들을 체크합니다.
if (calledActivity.checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|| calledActivity.checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|| calledActivity.checkSelfPermission(android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
|| calledActivity.checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| calledActivity.checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| calledActivity.checkSelfPermission(android.Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
if (calledActivity.shouldShowRequestPermissionRationale(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// shouldShowRequestPermissionRationale()가 true일 경우, 해당 권한이 필요한지 설명해주고, 권한 허가를 요청
// -> onRequestPermissionsResult()
// 권한 재요청
AlertDialog.Builder builder = new AlertDialog.Builder(calledActivity);
builder.setMessage("권한을 확인 해주셔야 앱이 제대로 작동합니다.");
builder.setTitle("권한 설정");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
calledActivity.requestPermissions(
new String[]{
Manifest.permission.CAMERA,
Manifest.permission.READ_CONTACTS,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE
},
1
);
}
});
builder.setNeutralButton("Cancel",null);
AlertDialog dialog = builder.create();
dialog.show();
} else {
calledActivity.requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.CAMERA, android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.RECORD_AUDIO}, 1);
}
} else {
//권한이 있는 경우
Toast.makeText(calledActivity, "권한 체크 확인됨", Toast.LENGTH_LONG).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grandResults) {
Log.d(getClass().getName(), "requestCode = " + requestCode);
switch (requestCode) {
case 1: {
if (grandResults.length > 0) {
for (int i = 0; i < permissions.length; i++) {
if (grandResults[i] != PackageManager.PERMISSION_GRANTED) {
Log.d(getClass().getName(), "거부된 경우");
Log.d(getClass().getName(), permissions[i]);
} else {
Log.d(getClass().getName(), "승인된 경우");
Log.d(getClass().getName(), permissions[i]);
}
}
}
}
}
}