๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’š ๊ฐœ๋ฐœ/์•ˆ๋“œ๋กœ์ด๋“œ

[์•ˆ๋“œ๋กœ์ด๋“œ] ๋Ÿฐํƒ€์ž„์— ๊ถŒํ•œ ์ฒดํฌํ•˜๋Š” ๋ฐฉ๋ฒ•

by devshin.kr 2022. 1. 5.
728x90

๋‘ ๊ฐ€์ง€ ์ƒ˜ํ”Œ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค.

Case 1. ํ•˜๋‚˜์˜ ๊ถŒํ•œ๋งŒ ์š”์ฒญํ•˜๋Š” ์ผ€์ด์Šค

Case 2. ๋‘ ๊ฐ€์ง€ ์ด์ƒ์˜ ๊ถŒํ•œ์„ ์š”์ฒญํ•˜๋Š” ์ผ€์ด์Šค

Case 1. ํ•˜๋‚˜์˜ ๊ถŒํ•œ๋งŒ ์š”์ฒญํ•˜๋Š” ์ผ€์ด์Šค ๋™์ž‘ ์˜ˆ์‹œ

ALLOW ๋ฅผ ๋ˆ„๋ฅด๋ฉด ๋ฐ”๋กœ ๊ถŒํ•œ์ด ๋ถ€์—ฌ ๋œ๋‹ค.
DENY > ์•Œ๋ฆผ์ฐฝ (ํ™•์ธ) > ์„ค์ • ์œผ๋กœ ์—ฐ๊ฒฐ๋˜๋ฉฐ ์‚ฌ์šฉ์ž๊ฐ€ ๊ถŒํ•œ์„ ์—ด์–ด์ค„ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

 

 

 

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. ๋‘ ๊ฐ€์ง€ ์ด์ƒ์˜ ๊ถŒํ•œ์„ ์š”์ฒญํ•˜๋Š” ์ผ€์ด์Šค ๋™์ž‘ ์˜ˆ์‹œ

ALLOW ํ•˜๋ฉด ๋ฐ”๋กœ ๊ถŒํ•œ์ด ๋ถ€์—ฌ ๋œ๋‹ค.
DENY > ์•Œ๋žŒ์ฐฝ (ํ™•์ธ) > ์„ค์ • ์œผ๋กœ ์—ฐ๊ฒฐ๋˜๋ฉฐ ์‚ฌ์šฉ์ž๊ฐ€ ๊ถŒํ•œ์„ ์ค„ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค.

 

 

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();
                }
        }
    }
}

๋Œ“๊ธ€