Tag: autostart

Add Setting to the BlackBerry Options

The Options application – or Settings on some BlackBerry themes – provides a unified location to set BlackBerry options and configure applications. A BlackBerry application can also add its own options to this listing, allowing a user to configure an application from outside of the application. This can be very useful for background applications that do not use a graphical user interface (GUI). The sample application below shows how to add custom options to the BlackBerry Options application by using the OptionsManager class and the OptionsProvider interface.

Note: This sample application uses an alternate entry point that specifies “auto-start” as its parameter. For more information on alternate entry points, see this article .

OptionsSample.java

package com.rim.samples.options;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.blackberry.api.options.*;

public class OptionsSample extends UiApplication
{
    public static void main(String[] args)
    {
        //This sample makes use of alternate entry points.
        //An alternate entry point will need to be configured
        //in your workspace to use this sample.

        if (args.length > 0 && args[0].equals("auto-start"))
        {
            //Enter the auto start portion of the application.
            //Register an options provider and exit.
            OptionsSampleOptionsProvider provider =
              new OptionsSampleOptionsProvider();
            OptionsManager.registerOptionsProvider(provider);
        }
        else
        {
            //Enter the GUI portion of the applicaiton.
            //Create a new instance of the application
            //and start the application on the event thread
            OptionsSample theApp = new OptionsSample();
            theApp.enterEventDispatcher();
        }
    }

    public OptionsSample()
    {
        MainScreen theScreen = new MainScreen();
        theScreen.setTitle(new LabelField("Options Sample"));
        OptionsSampleProperties props = OptionsSampleProperties.fetch();
        RichTextField greeting = new RichTextField();

        if (props.isGreetingEmphasized())
        {
            // An emphatic greeting is in all capitals and followed by
            // exclamation marks and is displayed in bold.
            String greetingText = props.getGreeting().toUpperCase()
              + "!!!!";
            Font emphatic = Font.getDefault().derive(Font.BOLD);
            int[] offsets = {0, greetingText.length()};
            byte[] attributes = {0};
            Font[] fonts = {emphatic};
            greeting.setText(greetingText, offsets, attributes, fonts);
        }
        else
        {
            greeting.setText(props.getGreeting());
        }

        theScreen.add(greeting);
        pushScreen(theScreen);
    }
}

OptionsSampleOptionsProvider.java

package com.rim.samples.options;

import net.rim.blackberry.api.options.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;

class OptionsSampleOptionsProvider implements OptionsProvider
{
    private EditField greetingEdit;
    private ObjectChoiceField styleChoice;
    private OptionsSampleProperties optionProperties;

    public OptionsSampleOptionsProvider()
    {
    }

    public String getTitle()
    {
        return "Options Sample";
    }

    public void populateMainScreen(MainScreen mainScreen)
    {
        //Read in the properties from the persistent store.
        optionProperties = OptionsSampleProperties.fetch();

        //Get the current values from optionProperties and create
        //the controls to represent them.
        greetingEdit = new EditField
          ("Greeting: ", optionProperties.getGreeting(),
            BasicEditField.DEFAULT_MAXCHARS, BasicEditField.NO_NEWLINE);
        mainScreen.add(greetingEdit);

        Object[] choices = {"Normal", "Emphatic!!"};
        int selection = optionProperties.isGreetingEmphasized() ? 1 : 0;
        styleChoice = new ObjectChoiceField("Style: ", choices, selection);
        mainScreen.add(styleChoice);
    }

    public void save()
    {
        //Get the new values from the UI controls
        //and set them in optionProperties.
        optionProperties.setGreeting(greetingEdit.getText());
        optionProperties.setGreetingEmphasized
          (styleChoice.getSelectedIndex() != 0);

        //Write our changes back to the persistent store.
        optionProperties.save();

        //Null out our member variables so that their objects can be garbage
        //collected. Note that this instance continues to be held by the
        //options manager even after the user exits the options app,
        //and will be re-used next time.

        greetingEdit = null;
        styleChoice = null;
        optionProperties = null;
    }
}

OptionsSampleProperties.java

package com.rim.samples.options;

import net.rim.device.api.system.*;
import net.rim.device.api.util.*;

//The configuration properties of the OptionsSample application. One instance holding
//the effective values resides in the persistent store.
class OptionsSampleProperties implements Persistable
{
    private String greeting;
    private boolean greetingEmphasized;

    //Hash of com.rim.training.options.OptionsSampleProperties
    private static final long PERSISTENCE_ID = 0x7eeffcc691f4418dL;

    //Persistent object wrapping the effective properties instance
    private static PersistentObject store;

    //Ensure that an effective properties set exists on startup.
    static {
        store = PersistentStore.getPersistentObject(PERSISTENCE_ID);
        synchronized (store) {
            if (store.getContents() == null) {
                store.setContents(new OptionsSampleProperties());
                store.commit();
            }
        }
    }

    // Constructs a properties set with default values.
    private OptionsSampleProperties()
    {
        greeting = "Hello there";
        greetingEmphasized = false;
    }

    //Retrieves a copy of the effective properties set from storage.
    public static OptionsSampleProperties fetch()
    {
        synchronized (store)
        {
            OptionsSampleProperties savedProps =
              (OptionsSampleProperties) store.getContents();
            return new OptionsSampleProperties(savedProps);
        }
    }

    //Causes the values within this instance to become the effective
    //properties for the application by saving this instance to the store.
    public void save()
    {
        synchronized (store)
        {
            store.setContents(this);
            store.commit();
        }
    }

    //The greeting is the text that is displayed within the
    //OptionsSample application.
    public String getGreeting()
    {
        return greeting;
    }

    public void setGreeting(String greetingLocal)
    {
        greeting = greetingLocal;
    }

    //If emphasized, the greeting is displayed in more resouding manner.
    public boolean isGreetingEmphasized()
    {
        return greetingEmphasized;
    }

    public void setGreetingEmphasized(boolean greetingEmphasizedLocal)
    {
        greetingEmphasized = greetingEmphasizedLocal;
    }

    //Cannonical copy constructor.
    private OptionsSampleProperties(OptionsSampleProperties other)
    {
        greeting = other.greeting;
        greetingEmphasized = other.greetingEmphasized;
    }
}

[1] http://supportforums.blackberry.com/t5/Java-Development/Add-application-options-to-the-BlackBerry-Options/ta-p/442859


auto-run startup application

BlackBerry application support auto-run at every startup.

if you want to create background application only use this [1]:

To create an auto-start application using the BlackBerry® Java® Plug-in for Eclipse®
  1. Double click on BlackBerry_App_Descriptor.xml found in the BlackBerry project.
  2. On the Application tab, check off Auto-run on startup.
  3. If you do not want an application icon to be shown on the home screen check the Do not display the application icon on the BlackBerry home screen check box.

To create an auto-start application using the BlackBerry JDE
   1. In the BlackBerry JDE, right-click the project and select Properties.
   2. On the Application tab, check the Auto-run on Startup check box.
   3. If you want your application to run in the background, and not appear on the ribbon, check the System Module check box.

else, if you want to create background application & ui application use this [2]:

The BlackBerry architecture allows applications to be started automatically in the background by the system without requiring the user to explicitly initialize them. This allows application threads to always run in the background with no user interface component. Alternate Entry Points can be used to launch the user interface (UI) component of the auto-started application when the user selects the icon from the ribbon. This feature allows application threads to be launched in the background that can be used for actions like listening for incoming push messages, pre-processing data, or initializing network communications with a server host.

After creating the projects for the original application, you will have to create another project for the UI entry point. Assuming that the thread to be run exists in the same project as the original application, follow these steps:

   1. Right-click the project node and select Properties.
   2. In the Properties window, select the Application tab.
   3. Verify the following options are checked: Auto-run on startup and System module (to register the thread with the system).
   4. Create another project under the same folder as the original project. Right-click the new project node and select Properties.
   5. Select the Application tab and select Alternate CLDC Application Entry Point from the Project type drop-down list. As shown in the attached file, select the name of the original project (for example, trafficreporter) from the Alternate entry point for drop-down list. Also specify the arguments that would launch the application using this alternate entry point (for example: gui).
   6. Modify the main() method of the original project as follows:

      public static void main(String[] args) {
           if ( args != null && args.length > 0 && args[0].equals("gui") ){
                // code to initialize the app
                theApp.enterEventDispatcher();
           } else {
                // code to launch the background thread }
           }
      }
   7. Add your application icon file to the this new entry point application and make it the ribbon icon.

The entry point application now consists only of an application icon and a parameter to pass to the main() method of the main application. There is no source code associated with this project. The main application contains all of the source code, including the thread to be started automatically.

Now, whenever the device is started, this main() method will be executed without any arguments, and the background thread will start. When the application icon is clicked from the main ribbon, the main() method will be executed with the argument specified in the above code sample ("gui") and the application can pop a screen to the foreground.

[1] http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800901/How_To_-_Configure_an_application_to_start_automatically_when_the_device_is_turned_on.html?nodeid=800920&vernum=0
[2] http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/796557/800738/800901/How_To_-_Setup_an_alternate_entry_point_for_my_application.html?nodeid=800820


  • Copyright © 1996-2010 vaks.in. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress