ν‹°μŠ€ν† λ¦¬ λ·°

Android

Android μ•± κΆŒν•œ 체크 및 ν—ˆμš©

λŒ•λŒ•μ΄λ°œπŸΎ 2018. 3. 22. 21:33

μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜ 개발 쀑에 νœ΄λŒ€ν° κ°€λŸ¬λ¦¬μ— μ ‘κ·Όν•˜μ—¬ ν”„λ‘œν•„μ„ μ„€μ •ν•˜λŠ” κΈ°λŠ₯이 μžˆλŠ”λ° 사진 데이터λ₯Ό κ°€μ Έμ˜€λ €λ©΄ 일단 μ•±μ˜ μ ‘κ·ΌκΆŒν•œμ΄ ν—ˆμš©λ˜μ–΄μžˆμ–΄μ•Ό ν•œλ‹€.


μˆœμ„œλ‘œ λ”°μ§€μžλ©΄




의 νλ¦„μœΌλ‘œ μ‹€ν–‰λœλ‹€. μ•ˆλ“œλ‘œμ΄λ“œ μŠ€νŠœλ””μ˜€μ—μ„œ μž‘μ„±λœ μ½”λ“œλ‘œ μ‚΄νŽ΄λ³΄μž.


1
2
3
4
private boolean checkPermissions() {
    int permissionState = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
    return permissionState == PackageManager.PERMISSION_GRANTED;
}
cs


κΆŒν•œ 보유 μ—¬λΆ€λ₯Ό ν™•μΈν•˜λŠ” λ©”μ†Œλ“œμΈ ActivityCompat.checkSelfPermission() λ₯Ό 호좜.


앱에 κΆŒν•œμ΄ μžˆλŠ” κ²½μš°μ—λŠ”  PackageManager.PERMISSION_GRANTEDλ₯Ό λ°˜ν™˜ν•˜κ³ , 앱이 μž‘μ—…μ„ 계속 진행할 수 μžˆμŠ΅λ‹ˆλ‹€. 

앱에 κΆŒν•œμ΄ μ—†λŠ” 경우 이 λ©”μ„œλ“œλŠ” PERMISSION_DENIEDλ₯Ό λ°˜ν™˜ν•˜κ³ , 앱이 μ‚¬μš©μžμ—κ²Œ λͺ…μ‹œμ μœΌλ‘œ κΆŒν•œμ„ μš”μ²­ν•΄μ•Ό ν•©λ‹ˆλ‹€.

μœ„μ— κ²½μš°μ—λŠ” 앱이 κΆŒν•œμ΄ μžˆμ„ λ•Œ true값을 return





1
2
3
4
private void startPermissionRequest() {
    ActivityCompat.requestPermissions(this,
            new String[] {Manifest.permission.READ_EXTERNAL_STORAGE} , REQUEST_PERMISSIONS_REQUEST_CODE);
}
cs


앱에 ν•„μš”ν•œ κΆŒν•œμ΄ μ—†λŠ” 경우 ν•„μš”ν•œ κΆŒν•œμ„ μš”μ²­ν•˜λŠ” λ©”μ†Œλ“œμΈ ActivityCompat.requestPermissions() λ₯Ό 호좜.






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private void requestPermissions() {
    boolean shouldProviceRationale =
            ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE);
 
    if( shouldProviceRationale ) {
        new android.support.v7.app.AlertDialog.Builder(this)
                .setTitle("μ•Œλ¦Ό")
                .setMessage("μ €μž₯μ†Œ κΆŒν•œμ΄ ν•„μš”ν•©λ‹ˆλ‹€.")
                .setPositiveButton("확인"new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        startPermissionRequest();
                    }
                })
                .create()
                .show();
    } else {
        startPermissionRequest();
    }
}
cs

앱에 κΆŒν•œμ΄ ν•„μš”ν•œ 이유λ₯Ό μ„€λͺ…ν•˜κ³ μž ν•˜λŠ” λ©”μ†Œλ“œ. 

μ‚¬μš©μžκ°€ μ„€λͺ…이 ν•„μš”ν•  μˆ˜λ„ μžˆλŠ” 상황을 찾도둝 Androidμ—μ„œλŠ” μœ ν‹Έλ¦¬ν‹° λ©”μ„œλ“œμΈ shouldShowRequestPermissionRationale()을 μ œκ³΅ν•©λ‹ˆλ‹€. 이전에 앱이 이 κΆŒν•œμ„ μš”μ²­ν–ˆκ³  μ‚¬μš©μžκ°€ μš”μ²­μ„ κ±°λΆ€ν•œ 경우, 이 λ©”μ„œλ“œλŠ” trueλ₯Ό λ°˜ν™˜ν•©λ‹ˆλ‹€.





확인 λ²„νŠΌ 클릭 μ‹œμ— onClick λ©”μ†Œλ“œλ₯Ό μ‹€ν–‰μ‹œμΌœμ„œ κ·Έ μ•ˆμ— μžˆλŠ” startPermissionRequest() λ©”μ†Œλ“œλ₯Ό μ‹€ν–‰μ‹œν‚¨λ‹€.



READ_EXTERNAL_STORAGE κΆŒν•œμ„ μš”μ²­ν•˜λ―€λ‘œ μœ„μ™€κ°™μ€ 창을 λ„μš°κ²Œ 됨


ν—ˆμš©μ‹œμ—λŠ” λ‹€μŒμž‘μ—…μ„ μ‹€ν–‰ν•˜κ² μ§€λ§Œ, κ±°λΆ€λ₯Ό ν•  μ‹œμ—λŠ” onRequestPermissionsResult λ©”μ†Œλ“œλ₯Ό μ‹€ν–‰ν•˜λ„λ‘ 

μ˜€λ²„λΌμ΄λ“œλ₯Ό ν•΄μ€˜μ•Όν•œλ‹€.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_PERMISSIONS_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0== PackageManager.PERMISSION_GRANTED) {
 
                } else {
                    new android.support.v7.app.AlertDialog.Builder(this)
                            .setTitle("μ•Œλ¦Ό")
                            .setMessage("μ €μž₯μ†Œ κΆŒν•œμ΄ ν•„μš”ν•©λ‹ˆλ‹€. ν™˜κ²½ μ„€μ •μ—μ„œ μ €μž₯μ†Œ κΆŒν•œμ„ ν—ˆκ°€ν•΄μ£Όμ„Έμš”.")
                            .setPositiveButton("확인"new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    Intent intent = new Intent();
                                    intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                                    Uri uri = Uri.fromParts("package",
                                            BuildConfig.APPLICATION_ID, null);
                                    intent.setData(uri);
                                    intent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(intent);
                                }
                            })
                            .create()
                            .show();
                }
        }
    }
 
cs

μ ‘κ·Ό ꢌ용 ν—ˆμš©μ„ κ±°λΆ€ ν–ˆμ„ λ•Œ ν•΄λ‹Ή λ©”μ†Œλ“œλ₯Ό νƒ€κ²Œ λ˜λŠ”λ° κ·Έλƒ₯ 경고창의 ν˜•μ‹μœΌλ‘œ 끝낼 μˆ˜λ„ μžˆμ§€λ§Œ, μœ„μ™€ 같은 ν˜•μ‹μœΌλ‘œ 

ν•΄λ‹Ή 앱에 섀정에 κ΄€ν•œ 창으둜 μ΄λ™μ‹œν‚¬ μˆ˜λ„ μžˆλ‹€. intent에 μ μš©ν•˜λŠ” 데이터 및 속성듀은 ν•΄λ‹Ή μ–΄ν”Œλ¦¬μΌ€μ΄μ…˜μ˜ 섀정창을 

μ΅œμƒμœ„ μ•‘ν‹°λΉ„ν‹°λ‘œ λ‚˜νƒ€λ‚˜κ²Œ ν•˜λŠ” ꡬ문이닀.




확인 λ²„νŠΌμ„ λˆ„λ₯Ό μ‹œμ— ν•΄λ‹Ή 앱에 κ΄€ν•œ μ„€μ • 창으둜 μ΄λ™ν•˜κ²Œ λœλ‹€.




'Android' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

android edit text underline ν„°μΉ˜ μ‹œ color λ³€ν™”μ£ΌκΈ°  (0) 2018.03.24
λŒ“κΈ€
곡지사항
μ΅œκ·Όμ— 올라온 κΈ€
μ΅œκ·Όμ— 달린 λŒ“κΈ€
Total
Today
Yesterday
링크
Β«   2024/11   Β»
일 μ›” ν™” 수 λͺ© 금 ν† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
κΈ€ 보관함