Android libraries are somewhat different from normal Java libraries in that you will often download the whole Eclipse project rather than a pre-compiled .jar file. That's why you need:
Installing the library
- Install a Git client for your computer
- Clone this repository by writing the following in the terminal. git clone https://github.com/pubsubio/pubsub-android
- Start Eclipse
- Right-click the Package Explorer area (don't right-click a project!) and select Import
- Select General and then Existing projects into workspace
- Select the Select root_directory radio button and press browse
- Navigate to, and select, the directory/folder where you cloned the pubsub-android library (see: point 2 in this list)
- Press Finish
Consuming the library
- Create your new Android project
- Right-click your project in the Package Explorer and select Properties
- Select Android and scroll down to the section called Library
- Press Add and select the library called pubsub_android
** Create the Pubsub object, this will be your main interaction channel with the Pubsub service.**
Pubsub mPubsub = new Pubsub( this, mHandler );
** All events in Pubsub.io are recievied through a Handler interface, register a new Handler and call it "mHandler". **
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case Pubsub.CONNECTED_TO_HOST:
// React when a connection attempt was successfull, for example:
// Subscribing or publishing
break;
case Pubsub.CONNECTION_FAILED:
// React when a connection attempt failed, for example:
// Issue another connection attempt in a few seconds.
break;
case Pubsub.CONNECTION_LOST:
// React when a connection was lost, for example:
// Inform the user and abort any pubsub-dependant tasks.
break;
}
}
};
** Subscribing to a topic. **
private static final int MY_FILTER = 1;
JSONObject filter = new JSONObject();
try {
filter.put("name", "value");
mPubsub.subscribe(filter, MY_FILTER);
} catch (JSONException e) {
e.printStackTrace();
}
** Publishing to a topic. **
JSONObject doc = new JSONObject();
try {
filter.put("name", "value");
mPubsub.publish(doc);
} catch (JSONException e) {
e.printStackTrace();
}
- Add a tag with the INTERNET rule.
- Add a tag with the ACCESS_NETWORK_STATE rule.