Lesson 3: Adding MobiLink support to your BlackBerry application

In this lesson, you add synchronization capabilities to your application.

 Add MobiLink synchronization capabilities to your BlackBerry application
  1. Update the HomeScreen class to add a Sync menu item.

    Double-click HomeScreen.java in the Package Explorer window, and then insert the following code above the try-catch statement that calls the getDataAccess method:

            // Add sync menu item
            addMenuItem(_syncMenuItem);
  2. Update the HomeScreen class to add a new method that defines the menu item in the class variable declarations.

    Insert the following code below the _addToListMenuItem method:

        private MenuItem _syncMenuItem = new MenuItem("Sync", 2, 1) {
            public void run() {
                onSync();
            }
        };
  3. Update the HomeScreen class to add the onSync method that is called in the previous step.

    Insert the following code below the onAddToList method:



        private void onSync() {
            try {
                if(_da.sync()) {
                    _statusLabel.setText("Synchronization succeeded");
                } else {
                    _statusLabel.setText("Synchronization failed");
                }
                refreshNameList();
            } catch (Exception ex) {
                Dialog.alert(ex.toString());
            }
        }
  4. Update the DataAccess class to define the _syncParms variable.

    Double-click DataAccess.java in the Package Explorer window, and then insert the following code below the private static DataAccess _da; call:

        private static SyncParms _syncParms;
  5. Update the DataAccess class to add a sync method.

    Insert the following code below the getNameVector method:

    Note

    You must replace your-host-name with your computer name. You cannot use this term in your application.



        public boolean sync() {
            try {
                if(_syncParms == null){
                    _syncParms = _conn.createSyncParms(SyncParms.HTTP_STREAM, 
                    				"mluser", 
                    				"HelloBlackBerrySyncModel");
                    _syncParms.setPassword("mlpassword");
                    _syncParms.getStreamParms().setHost("your-host-name"); // USE YOUR OWN
                    _syncParms.getStreamParms().setPort(8081); // USE YOUR OWN
                }
                _conn.synchronize(_syncParms);
                return true;
            }
            catch(ULjException uex) {
                Dialog.alert("Exception: " + uex.toString());
                return false;
            }
        }

    The synchronization parameters object, _syncParms, includes the user name and password that you specified when deploying the synchronization model. It also includes the name of the synchronization model you created. In MobiLink, this name can refer to the synchronization version or a set of synchronization logic that was deployed to your consolidated database.

    The stream parameters object, StreamHTTPParms, indicates the host name and port number of the MobiLink server. When you start the MobiLink server in the next lesson, use your own computer name for simulator testing and select a port that is available.

    Note

    When using a device, use an externally visible computer or a computer that is accessible from the BlackBerry Enterprise Server your device is paired to, such as the Sybase-hosted Relay Server. For more information about the Relay Server, see Introduction to the Relay Server.

  6. Click File » Save All.