In this lesson, you add synchronization capabilities to your application.
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); |
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(); } }; |
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()); } } |
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; |
Update the DataAccess class to add a sync method.
Insert the following code below the getNameVector method:
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.
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.
Click File » Save All.
Discuss this page in DocCommentXchange.
|
Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |