728x90
๋ ๊ฐ์ง ์ํ ์ฝ๋๊ฐ ์๋ค.
Case 1. ํ๋์ ๊ถํ๋ง ์์ฒญํ๋ ์ผ์ด์ค
Case 2. ๋ ๊ฐ์ง ์ด์์ ๊ถํ์ ์์ฒญํ๋ ์ผ์ด์ค
Case 1. ํ๋์ ๊ถํ๋ง ์์ฒญํ๋ ์ผ์ด์ค ๋์ ์์
Case 1. ํ๋์ ๊ถํ๋ง ์์ฒญํ๋ ์ผ์ด์ค ์ฝ๋
์ด ๋ถ๋ถ๋ง ์ถ๊ฐํด ์ค ๊ฒ
<uses-permission android:name="android.permission.CAMERA"/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.devshin93.runtimepermission">
<uses-permission android:name="android.permission.CAMERA"/>
<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/Theme.RuntimePermission">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.devshin93.runtimepermission;
import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity {
private static final int SINGLE_PERMISSION = 1004;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
runtimeCheckPermission();
}
public void runtimeCheckPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, SINGLE_PERMISSION);
} else {
Log.i("๊ถํ ํ
์คํธ", "์ฒ์๋ถํฐ ๊ถํ์ด ์์์ด์.");
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case SINGLE_PERMISSION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// ๊ถํ์ด ์ด๋ฏธ ์๋ ๊ฒฝ์ฐ
Log.i("๊ถํ ํ
์คํธ", "์ฌ์ฉ์๊ฐ ๊ถํ์ ๋ถ์ฌํด ์คฌ์ต๋๋ค.");
} else {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("์ฑ ๊ถํ ์ค์ ");
alertDialog.setMessage("์ค์ ์ผ๋ก ์ด๋ํฉ๋๋ค.");
alertDialog.setPositiveButton("ํ์ธ",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// ์ด ๋ถ๋ถ์ ์ค์ ์ผ๋ก ์ด๋ํ๋ ์ฝ๋์ด๋ฏ๋ก ์๋๋ก์ด๋ ์ด์์ฒด์ ๋ฒ์ ์ ๋ฐ๋ผ ์์ดํ ์ ์๋ค.
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).setData(Uri.parse("package:" + getApplicationContext().getPackageName()));
startActivity(intent);
dialogInterface.cancel();
}
});
alertDialog.setNegativeButton("์ทจ์", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
alertDialog.show();
}
}
}
}
Case 2. ๋ ๊ฐ์ง ์ด์์ ๊ถํ์ ์์ฒญํ๋ ์ผ์ด์ค ๋์ ์์
Case 2. ๋ ๊ฐ์ง ์ด์์ ๊ถํ์ ์์ฒญํ๋ ์ผ์ด์ค ์ฝ๋
์ฌ๊ธฐ์๋ ์ธ๋ถ์ ์ฅ์(sdcard) ์ read/write ๊ถํ์ ๋ถ์ฌํ๋๋ก ํ๋ค. Android API Level 30 ๋ถํฐ๋ ์ ์ฉ์ด ์ ๋๋ ๋ค๋ฅธ ๊ถํ์ผ๋ก ํ ์คํธ ํด ๋ณด๊ฑฐ๋, ํ ์คํธ ๋จ๋ง๊ธฐ/์ ๋ฎฌ๋ ์ดํฐ์ ๋ฒ์ ์ ๋ฎ์ถ์.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.devshin93.runtimepermissions">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<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/Theme.RunTimePermissions">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.devshin93.runtimepermissions;
import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
public class MainActivity extends AppCompatActivity {
private static final int MULTIPLE_PERMISSION = 1004;
/**
* ์ธ๋ถ์ ์ฅ์ (sdcard) ์ read/write ํ๋ ์ํ์
๋๋ค.
* Android API level 30 ๋ถํฐ๋ sdcard ์ ์ ๊ทผ์ด ๋ถ๊ฐํ ์ ์์ต๋๋ค.
* ํ
์คํธํ๋ ๋จ๋ง๊ธฐ/์ ๋ฎฌ๋ ์ดํฐ์ ๋ฒ์ ์ 30 ๋ฏธ๋ง์ผ๋ก ๋ฎ์ถ๊ฑฐ๋
* ์๋ PERMISSIONS ๋ชฉ๋ก์ CAMERA ๋ฑ์ผ๋ก ๋ณ๊ฒฝํ์๋ฉด ๋ฉ๋๋ค.
*/
private String[] PERMISSIONS = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!runtimeCheckPermission(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, MULTIPLE_PERMISSION);
} else {
// ๊ถํ์ด ์๋ ๊ฒฝ์ฐ..
Log.i("๊ถํ ํ
์คํธ", "๊ถํ์ด ์๋ค์");
}
}
public boolean runtimeCheckPermission(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MULTIPLE_PERMISSION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// ๊ถํ์ด ์๋ค๋ฉด~
Log.i("๊ถํ ํ
์คํธ", "์ฌ์ฉ์๊ฐ ๊ถํ์ ๋ถ์ฌํด ์คฌ์ต๋๋ค.");
} else {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("์ฑ ๊ถํ ์ค์ ");
alertDialog.setMessage("์ค์ ์ผ๋ก ์ด๋ํฉ๋๋ค.");
alertDialog.setPositiveButton("ํ์ธ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// ์ด ๋ถ๋ถ์ ์ค์ ์ผ๋ก ์ด๋ํ๋ ์ฝ๋์ด๋ฏ๋ก ์๋๋ก์ด๋ ์ด์์ฒด์ ๋ฒ์ ์ ๋ฐ๋ผ ์์ดํ ์ ์๋ค.
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).setData(Uri.parse("package:" + getApplicationContext().getPackageName()));
startActivity(intent);
dialogInterface.cancel();
}
});
alertDialog.setNegativeButton("์ทจ์", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
alertDialog.show();
}
}
}
}
๋๊ธ