如何在没有AppCompatActivity的情况下使用请求

时间:2019-09-30 03:59:25

标签: android kotlin

我想制作和使用仅包含许可相关功能的类。 (IDK如何解释,权限使用功能组)

我希望class不使用AppCompatActivity

所以我做得不好,而且行得通! 但是我认为使用AppCompatActivity作为参数是……可能不是好的体系结构。 (我知道这不是我现在的水平。但是我想为我的代码考虑一下)

我试图找到如何在没有AppCompatActivity的情况下使用ActivityCompat.requestPermissions()

  • MY_PERMISSION(per_type)为枚举

我以前是C ++的用户...对不起我的代码风格T0T ...我会尽快修复

class PermissionFunc
(It does not have any other class, inheritance, implements, extends(?))
{
... (skip)


private fun checkPermission(appCompactActivity: AppCompatActivity, per_type: MY_PERMISSION): Boolean
{
    var bReady = false

    if (ContextCompat.checkSelfPermission(appCompactActivity, callGetTypeString(per_type))
                                                        != PackageManager.PERMISSION_GRANTED)
    {
        //퍼미션을 보유중이지 않을 경우
        //알림창을 별도로 띄워준다.
        //If it does not have permission, popup dialog
        GeneralFunc()
            .CallCreateAlertDialog(appCompactActivity, appCompactActivity.getString(R.string.TEXT_NOTICE),
                appCompactActivity.getString(R.string.TEXT_PERMISSION_NOTICE, getPermissionName(per_type)),
            false, per_type)
    }
    else
    {
        bReady = true
    }

    return bReady
}

private fun CreateAlertDialog(appCompactActivity: AppCompatActivity, s_dialog_title : String,
                              s_dialog_message : String, add_negative_btn : Boolean = false,
                              per_type: MY_PERMISSION = MY_PERMISSION.E_NONE
)
{
    var str_title = s_dialog_title
    var str_message = s_dialog_message
    val builder = AlertDialog.Builder(appCompactActivity)

    if(str_title.isNullOrEmpty()||str_message.isNullOrEmpty())
    {
        return
    }

    builder.setTitle(str_title)
    builder.setMessage(str_message)
    builder.setCancelable(false)

    //per_type가 비어있지 않다면 퍼미션 요청용 Dialog이다.
    //if per_type is not empty, it is for permission
    if(per_type != MY_PERMISSION.E_NONE)
    {
        //popup dialog. if it receive btn OK -> request permission
        //요청 팝업을 띄워준다. 버튼을 누르면 리퀘스트.
        builder.setPositiveButton(R.string.BTN_OK)
        {
                dialogInterface, i -> PermissionFunc()
            .callRequestPermission(appCompactActivity, per_type)
        }
    }
    else
    {
        builder.setPositiveButton(R.string.BTN_OK)
        {
                dialogInterface, i -> dialogInterface.cancel()
        }
    }


    if(add_negative_btn)
    {
        builder.setNegativeButton(R.string.BTN_CANCEL)
        {
                dialogInterface, i -> dialogInterface.cancel()
        }
    }

    val dialog = builder.create()
    dialog.show()
}

private fun requestPermission(appCompactActivity: AppCompatActivity, per_type: MY_PERMISSION)
{
    ActivityCompat.requestPermissions(appCompactActivity,
        arrayOf(callGetTypeString(per_type)), callGetTypeCode(per_type))
}

...(跳过) }

它没有错误,但我想改进代码。

0 个答案:

没有答案