티스토리 뷰

반응형

[android/java] 앱 권한 요청하기(requestPermissions)

 

#권한 등록

[AndroidManifest.xml]

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

 

#권한 상태 체크 팝업 띄우기

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
    if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_DENIED){
        requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},10);
    }
}

 

#결과 값에 대한 처리

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode){
        case 10:
            if(grantResults[0]==PackageManager.PERMISSION_DENIED){
                Toast.makeText(this,"위치 권한 성공",Toast.LENGTH_LONG).show();
            }
            else{
                Toast.makeText(this,"위치 권한 성공",Toast.LENGTH_LONG).show();
            }
            break;
    }
}

 

#andorid 권한 요청 #앱 권한 요청 #requestPermissions #permission #권한 #java #android #안드로이드 #자바

반응형
댓글
반응형