Android Permission HaloPermission Detailed Use

  • 2021-08-31 09:11:17
  • OfStack

STEP 1 Regular use

Request 1 permission and receive the result callback


HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
      .setListener(object: PermissionListener{
        override fun onPermissionDenied(permissions: List<String>) {
          {your code for deny}
        }
        override fun onPermissionGrand(permissions: List<String>) {
          {your code for grand}
        }
      }).run()

Request multiple permissions


HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CALL_PHONE)
      .{ Omit code }
      
    //or
    
    val permissions:Array<String> = arrayOf("","")
    HoloPermission.with(this,*permissions)
      .{ Omit code }

Only care about callbacks whose permissions are allowed (not allowed)


HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
      .setGrandAction(object:GrandAction{
        override fun onPermissionGrand(permissions: List<String>) {
          {your code for grand}
        }

      }).run()

2. Use of RationaleRender

If you want to explain the reason for requesting permission to the user, you can use setRationaleRender method


HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
            .{ Omit callback setting code }
            .setRationaleRender(" To ensure the normal use of the function, please allow the next permission request application. ")
            .run()

If you want to customize the style of RationaleRender, such as:


HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
          .{ Omit callback setting code }
          .setRationaleRender(object:RationaleRender{
            override fun show(ctx: Context, permission: List<String>, process: RationaleRender.Process) {
              // Custom uses 1 A `Toast` Display information. 
              Toast.makeText(ctx," To ensure the normal use of the function, please allow the next permission request application. ",Toast.LENGTH_SHORT).show()

              //** In order to ensure that the subsequent process continues to execute, you need to call at an appropriate time process Adj. `onNext` Or `onCancel` Method **
              process.onNext()

              //onNext() Indicates the continuation of subsequent execution 
              //onCancel Will cancel the execution of the process and eventually call back onPermissionDenied Method 
            }
          })
          .run()

Instructions for triggering this callback:

If app previously requested this permission and the user denied it, this method calls back. This method does not call back if the option "Don 't ask again" is checked in the dialog box when the user denied permission before If the device policy prohibits the application from having this permission, this method will not call back

3. Use of SettingRender

If you want to explain the reason for requesting permission to the user, you can use setRationaleRender method


HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
         .{ Omit callback setting code }
         .setSettingRender(" Unable to use external storage, please set permissions to use it. ")
         .run()

If you want to customize the style of SettingRender, such as:


HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
          .{ Omit callback setting code }
          .setSettingRender(object:SettingRender{
            override fun show(ctx: Context, permission: List<String>, process: SettingRender.Process) {
              // Custom uses 1 A `Toast` Display information. 
              Toast.makeText(ctx," Unable to use external storage, please set permissions to use it. ",Toast.LENGTH_SHORT).show()

              //** In order to ensure that the subsequent process continues to execute, you need to call at an appropriate time process Adj. `onNext` Or `onCancel` Method **
              process.onNext()

              //onNext() Indicates the continuation of subsequent execution ,HaloPermission The system application permission setting interface will be opened 
              //onCancel The execution of the process will be canceled, the system application permission setting interface will not be opened, and eventually the callback will be made onPermissionDenied Method 
            }
          })
          .run()

If you feel that the permission setting interface opened by HaloPermission is not satisfactory to you, you can override the getCustomSettingIntent method of SettingRender to provide 1 Intent. If you return null, it will be opened in the default way of HaloPermission:


HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
          .{ Omit callback setting code }
          .setSettingRender(object:SettingRender{
            override fun show(ctx: Context, permission: List<String>, process: SettingRender.Process) {
              { Omitted code }
            }

            // Customize SettingIntent
            override fun getCustomSettingIntent(ctx: Context): Intent? {
                        return super.getCustomSettingIntent(ctx)
            }
          })
          .run()

4. Customize permission verification rules

It can be achieved in two steps


   //1.  Create a custom PermissionChecker
    class CustomChecker:PermissionChecker{
      override fun isPermissionGranted(ctx: Context, permission: String): Boolean {
        { Use your rules }
      }
    }
    
    //2.  Using custom rules 
    HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
      .{ Omit regular code }
      .run(CustomChecker())

Unless you are very sure, it is not recommended to use custom permission verification rules, because HaloPermission will adapt and be compatible as much as possible

5. Customize the request method

By default, ShadowActivity requests permission in the form of ShadowActivity. Of course, as long as you like, you can use Fragment to implement it. HaloPermission itself also provides Fragment's request mode, but this part of the implementation is finally removed, because for the use mechanism of Fragment, if it is used improperly, there may be some strange problems, which I think you and I don't want to see. Similarly, you can customize the request mode in two steps


    //1.  Create a custom PermissionCaller
    class CustomCaller: PermissionCaller{
       override fun requestPermission(ctx: Context, responder: PermissionResponder, vararg permision: String) {
         { Can be imitated HaloPermission Implementation, eventually calling at the appropriate time responder Let the process work normally }
       }
    }
    
    //2.  Using custom rules 
    HoloPermission.with(this,Manifest.permission.WRITE_EXTERNAL_STORAGE)
      .{ Omit regular code }
      .run(CustomCaller())

Related articles: