how to create a custom dialog for asking permissio

2019-08-02 20:01发布

how to create a custom dialog for asking permission in blackberry and store in application Descriptor. i have created this code but i am not able get our custom message and getting Exception Uncaught Exception:no Application Instance.

class PermissionTest extends UiApplication {

    private static String[] dialogChoice = { "Exit" };

    static boolean permissionsOk = false;

    // main method
    public static void main(String[] args) {
        _assertHasPermissions();
        PermissionTest theApp = new PermissionTest();
        theApp.enterEventDispatcher();
    }

    public PermissionTest() {
        if (permissionsOk)
            UiApplication.getUiApplication().pushScreen(new HomeScreen());
        else
            UiApplication.getUiApplication().pushScreen(new PermissionScreen());
    }

    // ASK FOR PERMISSIONS
    private static void _assertHasPermissions() {

        try {
            // Capture the current state of permissions and check against the
            // requirements.
            ApplicationPermissionsManager apm = ApplicationPermissionsManager
                    .getInstance();
            ApplicationPermissions original = apm.getApplicationPermissions();

            // Set up and attach a reason provider
            apm.addReasonProvider(
                    ApplicationDescriptor.currentApplicationDescriptor(),
                    new ReasonProvider() {
                        public String getMessage(int i) {
                            String msg = "I need the permissions in order to work properly.";
                            switch (i) {
                            case ApplicationPermissions.PERMISSION_LOCATION_API:
                                msg = "Needed for location based services.";
                                break;
                            case ApplicationPermissions.PERMISSION_BROWSER_FILTER:
                                msg = "Needed for location based services.";
                                break;
                            case ApplicationPermissions.PERMISSION_EMAIL:
                                msg = "Needed for location based services.";
                                break;
                            }
                            return msg;
                        }
                    });

            if (original
                    .getPermission(ApplicationPermissions.PERMISSION_LOCATION_API) == ApplicationPermissions.VALUE_ALLOW
                    && original
                            .getPermission(ApplicationPermissions.PERMISSION_BROWSER_FILTER) == ApplicationPermissions.VALUE_PROMPT
                    && original
                            .getPermission(ApplicationPermissions.VALUE_ALLOW) == ApplicationPermissions.VALUE_ALLOW) {
                permissionsOk = true;
            } else {
                // Create a permission request for each of the permissions your
                // application
                // needs. Note that you do not want to list all of the possible
                // permission
                // values since that provides little value for the application
                // or the user.
                // Please only request the permissions needed for your
                // application.
                ApplicationPermissions permRequest = new ApplicationPermissions();
                permRequest
                        .addPermission(ApplicationPermissions.PERMISSION_LOCATION_API);
                permRequest
                        .addPermission(ApplicationPermissions.PERMISSION_BROWSER_FILTER);
                permRequest
                        .addPermission(ApplicationPermissions.PERMISSION_EMAIL);

                permissionsOk = apm.invokePermissionsRequest(permRequest);
            }

            /*
             * if (permissionsOk) { // do nothing } else { // exit
             * System.exit(0); }
             */} catch (Exception e) {
            UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                    Dialog.ask(
                            "Application install error, Please re-install application",
                            dialogChoice, 0);
                }
            });
        }
    }
}

标签: blackberry
2条回答
我欲成王,谁敢阻挡
2楼-- · 2019-08-02 20:42

You can't, apm.invokePermissionsRequest() always shows the default dialog box.

A ReasonProvider only provides the description string that is displayed if the permission is set to PROMPT and the user has to confirm it.

查看更多
相关推荐>>
3楼-- · 2019-08-02 20:44

Did you try like this:

int k=Dialog.ask(Dialog.D_OK_CANCEL, "Can I provide all Permission?");
if(k==Dialog.D_OK)
{
    //Provide Permission;
}
查看更多
登录 后发表回答