- AndroidManifest.xml 파일 내용 확인
- 붉은색 부분이 필요한 권한.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="simple.app.selfpermission">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
레퍼런스 : https://developer.android.com/guide/topics/permissions/overview?hl=ko
2. 권한 요청
- 권한 확인 : ContextCompat.checkSelfPermission
- 권한 요청 : ActivityCompat.requestPermissions
public void checkSelfPermission() {
String temp = "";
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
temp += Manifest.permission.WRITE_EXTERNAL_STORAGE + " ";
}
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
temp += Manifest.permission.READ_PHONE_STATE + " ";
}
if (TextUtils.isEmpty(temp) == false) {
// 권한 요청
ActivityCompat.requestPermissions(this, temp.trim().split(" "), REQUEST_CODE_PERMISSIONS);
} else {
// 모두 허용 상태
}
}
3. 권한 요청 응답 확인
- onRequestPermissionsResult 오버라이드
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
// 권한 처리
case REQUEST_CODE_PERMISSIONS: {
int length = permissions.length;
for (int i = 0; i < length; i++) {
if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
// 동의
Log.d(TAG, "PERMISSION_GRANTED : " + permissions[i]);
}
}
return;
}
default:
return;
}
}
4. 샘플코드
소스코드
APK