모바일 프로그래밍/안드로이드 프로그래밍

android 권한체크

zelkova 2020. 4. 6. 16:10

<목차로 돌아가기>

 

 

gradle에 아래 추가

 

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
    applicationId "com.example.kepcomenpowermanager"
    minSdkVersion 19
    targetSdkVersion 29
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'gun0912.ted:tedpermission:2.2.3'

    implementation project(path: ':progressbarlinepercent')
}

 

 

 

 

[AndroidManifest.xml]에 필요한 권한 추가

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kepcomenpowermanager">

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_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/AppTheme">
<activity android:name=".Intro">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

 

 

 

[소스코드에 추가]

TedPermission.with(getApplicationContext())
        .setPermissionListener(permissionListener)
        .setRationaleMessage("카메라 권한이 필요합니다.")
        .setDeniedMessage("거부하셨습니다.")
        .setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
        .check();

 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 권한 체크
    TedPermission.with(getApplicationContext())
            .setPermissionListener(permissionListener)
            .setRationaleMessage("카메라 권한이 필요합니다.")
            .setDeniedMessage("거부하셨습니다.")
            .setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA)
            .check();
}

 


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RESULT_OK) {
        try {            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

PermissionListener permissionListener = new PermissionListener() {

    @Override
    public void onPermissionGranted() {
        Toast.makeText(getApplicationContext(), "권한이 허용됨",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onPermissionDenied(List<String> deniedPermissions) {
        Toast.makeText(getApplicationContext(), "권한이 거부됨", Toast.LENGTH_SHORT).show();
    }

};

 

 

 

 

<목차로 돌아가기>




반응형