본문 바로가기

OS/Android

안드로이드 PermissionManager library

반응형

전에 올렸던 권한 요청 소스는 특정 핸드폰에서는 제대로 작동하지 않는 이슈를 확인하게 되어 권한 관련 라이브러리를 포스팅 해보려고 한다.

library name : PermissionManager

1. Module: app Gradle에 implementation "com.github.karanchuri:PermissionManager:0.1.0"을 추가

2. repositories에 maven { url 'https://jitpack.io' }추가

3. manifest application 태그에 tools:replace="android:label" 추가

4. 엑티비티에 권한 요청 소스 작성

class StartActivity : AppCompatActivity()
{
	val arr_permission = mutableListOf<String>(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
    val HAHA_FOLDER_NAME = "/Download/HAHA/"
    
	override fun onCreate(savedInstanceState: Bundle?)
    {
    	super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_start)
        permission.checkAndRequestPermissions(this)
    }
    
    val permission = object: PermissionManager()
    {
        override fun ifCancelledAndCanRequest(activity: Activity?) 
        {
            super.ifCancelledAndCanRequest(activity)
        }

        override fun ifCancelledAndCannotRequest(activity: Activity?) 
        {
            super.ifCancelledAndCannotRequest(activity)
        }
        override fun setPermission(): MutableList<String> 
        {
            return arr_permission
        }
    }
    
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray)
    {
        permission.checkResult(requestCode, permissions, grantResults)
        val granted = permission.status.get(0).granted
        val denied = permission.status.get(0).denied

        if(granted.size == arr_permission.size)
        {
            makeHAHADATAFolder()
        }
    }
    
    fun makeHAHADATAFolder()
    {
    	var f = File(Environment.getExternalStorageDirectory(), HAHA_FOLDER_NAME)
        if(!f.exists())
        {
            f.mkdir()
        }
    }
 }
 
    

 

반응형