2012-05-03 58 views
1

如何創建一個自定義對話框來詢問黑莓的權限並存儲在應用描述符中。我已經創建了這個代碼,但我無法獲得我們的自定義消息,並且獲取了未捕獲的異常異常:沒有應用程序實例。如何創建一個自定義對話框來詢問黑莓的權限

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); 
       } 
      }); 
     } 
    } 
} 

回答

0

你嘗試這樣的:

int k=Dialog.ask(Dialog.D_OK_CANCEL, "Can I provide all Permission?"); 
if(k==Dialog.D_OK) 
{ 
    //Provide Permission; 
} 
+0

我的問題是如何問自定義消息的權限.. –

1

你不能,apm.invokePermissionsRequest()始終顯示的默認對話框。

甲ReasonProvider僅提供,如果權限設置爲迅速和用戶必須確認所顯示的描述的字符串。

+0

但我無法得到我的描述字符串,我已通過。我正在與JRE 4.5一起工作,是否有任何問題。 –