This is the fourth article in the BlackBerry Application Developer guide series, "BlackBerry Application Developer guide option API". The blackberry option api in net. rim. BlackBerry. API. options allows you to add an option entry to the device option.
With this function, you can add a system-level option to the BlackBerry devices that can be used by multiple programs.
When you click the Options icon on the home screen, a column of Options will appear, such as AutoText, Date/Time, and firewall. you can select these items to view the screen of an option. this screen displays one or more fields. generally, you can change the value of each field.
Add Option
Add register Option
The OptionsProvider interface allows applications to add options. It includes the getTitle (), save (), and populateMainScreen () implementations.
Add the option when the BlackBerry device starts.
Create a class library project, which has a libMain () method to complete the registration.
Create a class library project
◆ Create a project in BlackBerry JDE.
◆ Right-click the project and click Properties.
◆ In the Properties window, click the Application tag.
◆ In the Project type drop-down list, click Library.
◆ Select Auto-run in the startup options.
◆ Click OK.
Register as an Options Provider
The implementation of getInstance () can get a static instance of a class. Only one instance exists at a time. Call the registerOptionsProvider () method of libMain (). Use the static instance of a class as the parameter.
- private static DemoOptionsProvider _instance;
- //...
- public static DemoOptionsProvider getInstance() {
- if(_instance == null) {
- _instance = new DemoOptionsProvider("Options Demo");
- }
- return _instance;
- }
- //...
- public static void libMain(String[] args) {
- OptionsManager.registerOptionsProvider(getInstance());
- }
Save Option
To save the selected option values, implement the Persistable interface. In your implementation, define the method to set the selected option values, and submit and obtain the option values in the persistent storage.
Note: If you implement the Persistable interface as an internal class, set the get (), set (), and commit () Methods to public, in this way, other applications can access the option data.
Provide option Data Access
Add a public method in your class library to allow other applications to access the option data.
Code instance
This example describes how to use the options.
Example: DemoOptionsProvider. java
- /**
- * DemoOptionsProvider.java
- * Copyright 2002-2005 Research In Motion Limited.
- */
- package com.rim.samples.docs.demooptionsprovider;
- import net.rim.blackberry.api.options.*;
- import net.rim.device.api.ui.component.*;
- import net.rim.device.api.ui.container.*;
- import net.rim.device.api.i18n.*;
- import net.rim.device.api.system.*;
- import net.rim.device.api.util.*;
-
- // A simple library class to demonstrate the use of the options facilities.
- public final class DemoOptionsProvider implements OptionsProvider {
- // members
- private ObjectChoiceField _ocf;
- private OptionsDemoData _data;
- private String _title;
- private static DemoOptionsProvider _instance;
-
- // constructors
- private DemoOptionsProvider() {
- }
-
- private DemoOptionsProvider(String title) {
- _title = title;
- _data = OptionsDemoData.load();
- }
-
- // Only allow one instance of this class.
- public static DemoOptionsProvider getInstance() {
- if (_instance == null) {
- _instance = new DemoOptionsProvider("Options Demo");
- }
- return _instance;
- }
-
- // On startup, create the instance and register it.
- public static void libMain(String[] args) {
- OptionsManager.registerOptionsProvider(getInstance());
- }
-
- // Get the title for the option item.
- public String getTitle() {
- return _title;
- }
-
- // Add fields to the screen.
- public void populateMainScreen(MainScreen screen) {
- int index = _data.getSelected();
- String[] choices = {“High”, “Low”, “None”};
- _ocf = new ObjectChoiceField(“Security: “, choices, index);
- screen.add(_ocf);
- }
-
- // Save the data.
- public void save() {
- _data.setSelected(_ocf.getSelectedIndex());
- _data.commit();
- }
-
- // Retrieve the data. Used by other applications to access options data.
- public OptionsDemoData getData() {
- return _data;
- }
-
- // Inner class to store selected option values.
- public static final class OptionsDemoData implements Persistable {
- private static final long ID = 0x6af0b5eb44dc5164L;
- private int _selectedOption;
- private OptionsDemoData() {
- }
-
- public int getSelected() {
- return _selectedOption;
- }
- public void setSelected(int index) {
- _selectedOption = index;
- }
-
- public void commit() {
- PersistentObject.commit(this);
- }
-
- private static OptionsDemoData load() {
- PersistentObject persist = PersistentStore.getPersistentObject(
- OptionsDemoData.ID );
- OptionsDemoData contents = (OptionsDemoData)persist.getContents();
- synchronized( persist ) {
- if( contents == null ) {
- contents = new OptionsDemoData();
- persist.setContents( contents );
- persist.commit();
- }
- }
- return contents;
- }
- }
- }