In this lesson, you add synchronization capabilities to your application.
In the HomeScreen constructor, add a Sync menu item.
// Add a menu item addMenuItem(_addToListMenuItem); // Add sync menu item addMenuItem(_syncMenuItem); // Create database and connect try{ ... |
Define the menu item in the class variables declarations.
private MenuItem _addToListMenuItem = new MenuItem("Add", 1, 1){ public void run() { onAddToList(); } }; private MenuItem _syncMenuItem = new MenuItem("Sync", 2, 1){ public void run() { onSync(); } }; |
Create the onSync method.
private void onSync(){ try{ if( _da.sync() ){ _statusLabel.setText("Synchronization succeeded"); } else { _statusLabel.setText("Synchronization failed"); } this.refreshNameList(); } catch ( Exception ex){ Dialog.alert( ex.toString() ); } } |
Define the syncParms and streamParms variables within the DataAccess class.
private static SyncParms _syncParms; private static StreamHTTPParms _streamParms; |
In the DataAccess class, add a sync method.
public boolean sync() { try { if(_syncParms == null){ String host = "relayserver.sybase.com/ias_relay_server/client/rs_client.dll/tomslee.HelloBlackBerry"; _syncParms = _conn.createSyncParms( "mluser", "HelloBlackBerrySyncModel" ); _syncParms.setPassword("mlpassword"); _streamParms = _syncParms.getStreamParms(); _streamParms.setPort( 8081 ); // use your own _streamParms.setHost( host ); // use your own if(host != null && _host.equals("relayserver.sybase.com/ias_relay_server/client/rs_client.dll/tomslee.HelloBlackBerry")) { _streamParms.setURLSuffix("/ias_relay_server/client/rs_client.dll/tomslee.HelloBlackBerry"); } } public boolean complete() { try{ _conn.checkpoint(); _conn.release(); _conn = null; _da = null; return true; } catch(Exception e){ 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 now refers to the synchronization version, or a set of synchronization logic, that was deployed to your consolidated database.
The stream parameters object, StreamHTTPParms, indicate 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. Do not use localhost as your computer name. You can use port 80 if you have no web server running on your computer.
When using a real device, use an externally visible machine or a machine that is accessible from the BlackBerry Enterprise Server your device is paired to, such as the Sybase hosted relay server. For more information on the relay server, see Introduction to the Relay Server.
Compile your application.
Discuss this page in DocCommentXchange.
|
Copyright © 2010, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.0 |