tencent cloud

Feedback

Custom Permission Requests

Last updated: 2024-11-21 17:32:20

    Mini program requests system permissions

    When using mini programs, some APIs require not only mini program authorization but also corresponding Android system permissions to function correctly. By default, the SDK will automatically prompt the user to grant these system permissions when needed. However, you can disable this automatic permission request during SDK initialization. In this case, the host app must handle these system permission requests to ensure the mini program APIs work properly.
    @ProxyService(proxy = MiniConfigProxy.class)
    public class MiniConfigProxyImpl extends MiniConfigProxy {
       /**
        * App Application
        * @
        */
       @Override
       public Application getApp() {
           return "app Application";
        }
     
         /**
        * Create initialization configuration information
        * @
        */
       @Override
       public MiniInitConfig buildConfig() {
         MiniInitConfig.Builder builder = new MiniInitConfig.Builder();
         MiniInitConfig config = builder
                   .configAssetName("tcmpp-android-configurations.json") //Configuration file name in assets
                    .imei("IMEI") //(Optional) Device ID, which is used for grayscale release and use of mini programs on the management platform
                    .autoRequestPermission(false) //onfigure whether to automatically request system permissions for APIs that require them
                    .debug(true) //Log switch, turned off by default.
                    .build();
        }
    }

    Customize system permission requests

    By implementing the IPermissionManagerProxy API, you can intercept and customize the logic for mini program system permission requests. The IPermissionManagerProxy API includes three methods: isPermissionGranted to check if the host has a specific system permission, and requestForPermission and requestForPermissions to notify the host to request one or multiple permissions.
    Note:
    Once you customize system permission requests, the autoRequestPermission setting in the initialization configuration will no longer be effective.
    @ProxyService(proxy = IPermissionManagerProxy.class)
    public class PermissionProxyImpl implements IPermissionManagerProxy {
     
       /**
        * Check if the host has a specific system permission.
        * @param context   Android context
        * @param permission    The system permission to check, refer to android.Manifest.permission
        * @return              Whether the host has permission
        */
       @Override
       public boolean isPermissionGranted(Context context, String permission) {
           return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
        }
     
       /**
        * Notify the host to request a system permission.
        * @param activity      The mini program Activity requesting the permission
        * @param permission     The system permission to request, refer to android.Manifest.permission
        * @param callbacks      Callback to return the permission request result to the mini program
        */
       @Override
       public void requestForPermission(Activity activity, String permission, RequestPermissionCallback callbacks) {
           Toast.makeText(activity,"applying for" + permission + "permission", Toast.LENGTH_LONG).show();
        }
     
       /**
        * Notify the host to request multiple system permissions.
        * @param activity      The mini program Activity requesting the permissions
        * @param permissions    The list of system permissions to request, refer to android.Manifest.permission
        * @param callbacks     Callback to return the permission request result to the mini program
        */
       @Override
       public void requestForPermissions(Activity activity, String[] permissions, RequestPermissionCallback callbacks) {
           Toast.makeText(activity,"applying for" + permissions[0] + "permissions," Toast.LENGTH_LONG).show();;
        }
    }
    /**
     * Notify the result of system permission requests.
     */
    interface RequestPermissionCallback {
       /**
        All system permissions are successfully granted.
        */
       void onSuccess();
     
       /**
        * Some or all system permissions are denied.
        * @param rejected      List of denied system permissions
        */
       void onFail(String[] rejected);
    }
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support