如何创建自定义对话框以在blackberry中询问权限并在应用程序描述符中存储。我已创建此代码但我无法获取自定义消息并获得Exception Uncaught Exception:无应用程序实例。
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 :(得分:1)
你不能,apm.invokePermissionsRequest()总是显示默认对话框。
如果权限设置为PROMPT且用户必须确认,则ReasonProvider仅提供显示的描述字符串。
答案 1 :(得分:0)
你是否尝试过这样:
int k=Dialog.ask(Dialog.D_OK_CANCEL, "Can I provide all Permission?");
if(k==Dialog.D_OK)
{
//Provide Permission;
}