BlackBerry Application Developer guide option API

Source: Internet
Author: User

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.

 
  1. private static DemoOptionsProvider _instance;  
  2. //...  
  3. public static DemoOptionsProvider getInstance() {  
  4. if(_instance == null) {  
  5.     _instance = new DemoOptionsProvider("Options Demo");  
  6. }  
  7. return _instance;  
  8. }  
  9. //...  
  10. public static void libMain(String[] args) {  
  11.     OptionsManager.registerOptionsProvider(getInstance());  
  12. } 

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

 
  1. /**  
  2. * DemoOptionsProvider.java  
  3. * Copyright 2002-2005 Research In Motion Limited.  
  4. */ 
  5. package com.rim.samples.docs.demooptionsprovider;  
  6. import net.rim.blackberry.api.options.*;  
  7. import net.rim.device.api.ui.component.*;  
  8. import net.rim.device.api.ui.container.*;  
  9. import net.rim.device.api.i18n.*;  
  10. import net.rim.device.api.system.*;  
  11. import net.rim.device.api.util.*;  
  12.  
  13. // A simple library class to demonstrate the use of the options facilities.  
  14. public final class DemoOptionsProvider implements OptionsProvider {  
  15.     // members  
  16.     private ObjectChoiceField _ocf;  
  17.     private OptionsDemoData _data;  
  18.     private String _title;  
  19.     private static DemoOptionsProvider _instance;  
  20.  
  21.     // constructors  
  22.     private DemoOptionsProvider() {  
  23.        }  
  24.  
  25.     private DemoOptionsProvider(String title) {  
  26.        _title = title;  
  27.        _data = OptionsDemoData.load();  
  28.        }  
  29.  
  30.     // Only allow one instance of this class.  
  31.     public static DemoOptionsProvider getInstance() {  
  32.        if (_instance == null) {  
  33.            _instance = new DemoOptionsProvider("Options Demo");  
  34.            }  
  35.        return _instance;  
  36.        }  
  37.  
  38.     // On startup, create the instance and register it.  
  39.     public static void libMain(String[] args) {  
  40.        OptionsManager.registerOptionsProvider(getInstance());  
  41.        }  
  42.  
  43.     // Get the title for the option item.  
  44.     public String getTitle() {  
  45.        return _title;  
  46.        }  
  47.  
  48.     // Add fields to the screen.  
  49.     public void populateMainScreen(MainScreen screen) {  
  50.        int index = _data.getSelected();  
  51.        String[] choices = {“High”, “Low”, “None”};  
  52.        _ocf = new ObjectChoiceField(“Security: “, choices, index);  
  53.        screen.add(_ocf);  
  54.        }  
  55.  
  56.     // Save the data.  
  57.     public void save() {  
  58.        _data.setSelected(_ocf.getSelectedIndex());  
  59.        _data.commit();  
  60.        }  
  61.  
  62.     // Retrieve the data. Used by other applications to access options data.  
  63.     public OptionsDemoData getData() {  
  64.        return _data;  
  65.        }  
  66.  
  67.     // Inner class to store selected option values.  
  68.     public static final class OptionsDemoData implements Persistable {  
  69.        private static final long ID = 0x6af0b5eb44dc5164L;  
  70.        private int _selectedOption;  
  71.        private OptionsDemoData() {         
  72.        }  
  73.  
  74.        public int getSelected() {  
  75.            return _selectedOption;  
  76.            }  
  77.        public void setSelected(int index) {  
  78.            _selectedOption = index;  
  79.            }  
  80.  
  81.        public void commit() {  
  82.            PersistentObject.commit(this);  
  83.            }  
  84.  
  85.        private static OptionsDemoData load() {  
  86.            PersistentObject persist = PersistentStore.getPersistentObject(  
  87.                   OptionsDemoData.ID );  
  88.            OptionsDemoData contents = (OptionsDemoData)persist.getContents();  
  89.            synchronized( persist ) {  
  90.               if( contents == null ) {  
  91.                   contents = new OptionsDemoData();  
  92.                   persist.setContents( contents );  
  93.                   persist.commit();  
  94.                   }  
  95.               }  
  96.            return contents;  
  97.            }  
  98.        }  
  99.   }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.