Getting started with Android Classic API
Create a client application within minutes that connects to the Diffusion™ server.
- Download the Android software development kit (SDK) and the Diffusion Android library.
- Create an Android project that references the diffusion-android-version.jar library, where version is the version number, for example 5.9.4.
-
In your project, create a Java™ file that extends the
android.app.Activity class.
import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView; import com.pushtechnology.android.diffusion.DiffusionClient; import com.pushtechnology.mobile.APIException; import com.pushtechnology.mobile.ConnectionDetails; import com.pushtechnology.mobile.DiffusionConnectionListener; import com.pushtechnology.mobile.DiffusionTopicStatusListener; import com.pushtechnology.mobile.MalformedURLException; import com.pushtechnology.mobile.Message; import com.pushtechnology.mobile.PingMessage; import com.pushtechnology.mobile.ServerDetails; import com.pushtechnology.mobile.ServiceTopicError; import com.pushtechnology.mobile.ServiceTopicHandler; import com.pushtechnology.mobile.ServiceTopicListener; import com.pushtechnology.mobile.ServiceTopicResponse; import com.pushtechnology.mobile.TopicListener; import com.pushtechnology.mobile.TopicMessage; import com.pushtechnology.mobile.URL; import com.pushtechnology.mobile.enums.EncodingValue; public class DemoClient extends Activity implements DiffusionConnectionListener, DiffusionTopicStatusListener { private DiffusionClient theClient; static final String TAG = "Diffusion Client"; private static final String SERVICE_TOPIC = "SERVICE"; ConnectionDetails cnxDetails; { try { ServerDetails svrDetailsArr[] = new ServerDetails[] { new ServerDetails( new URL( "URL:port" ) ), }; cnxDetails = new ConnectionDetails( svrDetailsArr ); cnxDetails.setTopics( SERVICE_TOPIC ); cnxDetails.setCascade( true ); cnxDetails.setAutoFailover( true ); } catch (MalformedURLException ex) { writeln( ex.toString() ); } } ServerDetails currentServerDetails; // Set Diffusion connection details, and place the Diffusion connection private void connectToServer() { theClient = new DiffusionClient(); theClient.setConnectionDetails( cnxDetails ); theClient.addTopicListener(new TopicListener()); theClient.setConnectionListener(this); theClient.setTopicStatusListener( this ); theClient.connect(); statusText.setText( String.format( "Connecting to %s", currentServerDetails.getUrl() ) ); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Set up the UI of your Android app connectToServer(); } }
- Import com.pushtechnology.android.diffusion.DiffusionClient and the classes in the com.pushtechnology.mobile package.
- Implement DiffusionConnectionListener and DiffusionTopicStatusListener.
- Create a ConnectionDetails object that includes the connections details and settings that you require.
- Create a connectToServer() method that uses the ConnectionDetails object to set the connections details and places a connection to Diffusion.
- Override the onCreate() method inherited from Activity. In this method call connectToServer() to place the Diffusion connection when the application starts.
- Use the onCreate() method to set up the views and content of your Android application.