1

如何创建自定义对话框以在黑莓中询问权限并存储在应用程序描述符中。我已经创建了此代码,但我无法获取我们的自定义消息并获取 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);
                }
            });
        }
    }
}
4

2 回答 2

1

你不能, apm.invokePermissionsRequest() 总是显示默认对话框。

ReasonProvider 仅提供在权限设置为 PROMPT 且用户必须确认时显示的描述字符串。

于 2012-05-04T06:56:12.107 回答
0

你试过这样吗:

int k=Dialog.ask(Dialog.D_OK_CANCEL, "Can I provide all Permission?");
if(k==Dialog.D_OK)
{
    //Provide Permission;
}
于 2012-05-03T14:30:39.213 回答