Skip to content

Commit 99236ab

Browse files
author
Andreas Göransson
committed
Fixed a threading issue and updated the Readme doc
1 parent b3937d2 commit 99236ab

File tree

9 files changed

+484
-478
lines changed

9 files changed

+484
-478
lines changed

.classpath

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<classpath>
3-
<classpathentry kind="src" path="src"/>
4-
<classpathentry kind="src" path="gen"/>
5-
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
6-
<classpathentry kind="output" path="bin/classes"/>
7-
</classpath>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="src" path="gen"/>
5+
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
6+
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
7+
<classpathentry kind="output" path="bin/classes"/>
8+
</classpath>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.6
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.6

README.md

Lines changed: 84 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,84 @@
1-
# pubsub-android
2-
3-
4-
## Installing and consuming Pubsub.io in Android
5-
**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:**
6-
7-
**Installing the library**
8-
9-
1. Install a Git client for your computer
10-
2. Clone this repository by writing the following in the terminal. **git clone https://github.com/pubsubio/pubsub-android**
11-
3. Start Eclipse
12-
4. **Right-click** the **Package Explorer** area (don't right-click a project!) and select **Import**
13-
5. Select **General** and then **Existing projects into workspace**
14-
6. Select the **Select root_directory** radio button and press **browse**
15-
7. Navigate to, and select, the directory/folder where you cloned the pubsub-android library (*see: point 2 in this list*)
16-
8. Press **Finish**
17-
18-
**Consuming the library**
19-
20-
1. Create your new Android project
21-
2. **Right-click** your **project** in the **Package Explorer** and select **Properties**
22-
3. Select **Android** and **scroll down** to the section called **Library**
23-
4. Press **Add** and select the library called **pubsub_android**
24-
25-
## Getting started with Pubsub.io for Android
26-
27-
**First, create a ServiceConnection in your activity. This is required because pubsub-android is a Service, and also where we define what hub & sub to connect to.**
28-
29-
``` java
30-
private ServiceConnection serviceConnection = new ServiceConnection() {
31-
@Override
32-
public void onServiceConnected(ComponentName className, IBinder service) {
33-
mPubsub = ((Pubsub.LocalBinder) service).getService();
34-
mPubsub.setHandler(mHandler);
35-
mPubsub.connect();
36-
}
37-
38-
@Override
39-
public void onServiceDisconnected(ComponentName className) {
40-
mPubsub = null;
41-
mPubsub.setHandler(null);
42-
}
43-
};
44-
```
45-
46-
**Now we can register callbacks that our app will react to, these are just plain integers (They should be unique!).**
47-
48-
``` java
49-
// Create your own callback id, random number. Make sure they don't have the same values as any of the Pubsub constants.
50-
private int MY_OWN_CALLBACK = 6322;
51-
52-
// Use our new callback id to subscribe to specific events on the sub
53-
JSONObject json_filter = new JSONObject();
54-
55-
// Our filter is currently looking for version numbers that are GREATER THAN 0.1!
56-
try {
57-
JSONObject version = new JSONObject();
58-
version.put("$gt", 0.1);
59-
60-
json_filter.put("version", version);
61-
} catch (JSONException e) {
62-
e.printStackTrace();
63-
}
64-
65-
mPubsub.subscribe(json_filter, MY_OWN_CALLBACK);
66-
```
67-
68-
**Then, create the Handler. This will act as a message callback between the Service and your Activity.**
69-
70-
``` java
71-
private Handler mHandler = new Handler() {
72-
@Override
73-
public void handleMessage(Message msg) {
74-
switch (msg.what) {
75-
case Pubsub.RAW_TEXT:
76-
byte[] readBuf = (byte[]) msg.obj;
77-
String readMessage = new String(readBuf, 0, msg.arg1);
78-
// TODO Do something with the message here...
79-
break;
80-
case Pubsub.TERMINATED:
81-
// TODO React when the connection fails...
82-
break;
83-
case Pubsub.ERROR:
84-
// TODO Read error messages here...
85-
break;
86-
case MY_OWN_CALLBACK:
87-
try {
88-
double version = doc.getDouble("version");
89-
Log.i(TAG, "Version message from Pubsub.io: " + version);
90-
} catch (JSONException e) {
91-
e.printStackTrace();
92-
}
93-
break;
94-
}
95-
}
96-
};
97-
```
98-
99-
**To start the service, you'll use an intent in the "onResume()" activity method.**
100-
101-
``` java
102-
@Override
103-
protected void onResume() {
104-
// Connect to the service
105-
Intent intent = new Intent(this, Pubsub.class);
106-
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
107-
108-
super.onResume();
109-
}
110-
```
111-
112-
**And make sure to disconnect from the service in "onPause()" too! Note, this will NOT shut down your connection.**
113-
114-
``` java
115-
@Override
116-
protected void onPause() {
117-
// Disconnect from the service
118-
unbindService(serviceConnection);
119-
120-
super.onPause();
121-
}
122-
```
123-
124-
## Also, you must make sure NOT to forget your AndroidManifest, it's imperative that you make the following changes!
125-
126-
* Add a <service> tag that points to the Pubsub service, otherwise you can't connect to it.
127-
* Add a <uses-permission> tag with the INTERNET rule.
128-
* Add a <user-permission> tag with the ACCESS_NETWORK_STATE rule.
1+
# pubsub-android
2+
3+
4+
## Installing and consuming Pubsub.io in Android
5+
**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:**
6+
7+
**Installing the library**
8+
9+
1. Install a Git client for your computer
10+
2. Clone this repository by writing the following in the terminal. **git clone https://github.com/pubsubio/pubsub-android**
11+
3. Start Eclipse
12+
4. **Right-click** the **Package Explorer** area (don't right-click a project!) and select **Import**
13+
5. Select **General** and then **Existing projects into workspace**
14+
6. Select the **Select root_directory** radio button and press **browse**
15+
7. Navigate to, and select, the directory/folder where you cloned the pubsub-android library (*see: point 2 in this list*)
16+
8. Press **Finish**
17+
18+
**Consuming the library**
19+
20+
1. Create your new Android project
21+
2. **Right-click** your **project** in the **Package Explorer** and select **Properties**
22+
3. Select **Android** and **scroll down** to the section called **Library**
23+
4. Press **Add** and select the library called **pubsub_android**
24+
25+
## Getting started with Pubsub.io for Android
26+
27+
** Create the Pubsub object, this will be your main interaction channel with the Pubsub service.**
28+
29+
``` java
30+
Pubsub mPubsub = new Pubsub( this, mHandler );
31+
```
32+
33+
** All events in Pubsub.io are recievied through a Handler interface, register a new Handler and call it "mHandler". **
34+
35+
``` java
36+
private Handler mHandler = new Handler() {
37+
@Override
38+
public void handleMessage(Message msg) {
39+
switch (msg.what) {
40+
case Pubsub.CONNECTED_TO_HOST:
41+
// React when a connection attempt was successfull, for example:
42+
// Subscribing or publishing
43+
break;
44+
case Pubsub.CONNECTION_FAILED:
45+
// React when a connection attempt failed, for example:
46+
// Issue another connection attempt in a few seconds.
47+
break;
48+
case Pubsub.CONNECTION_LOST:
49+
// React when a connection was lost, for example:
50+
// Inform the user and abort any pubsub-dependant tasks.
51+
break;
52+
}
53+
}
54+
};
55+
```
56+
57+
** Subscribing to a topic. **
58+
``` java
59+
private static final int MY_FILTER = 1;
60+
61+
JSONObject filter = new JSONObject();
62+
try {
63+
filter.put("name", "value");
64+
mPubsub.subscribe(filter, MY_FILTER);
65+
} catch (JSONException e) {
66+
e.printStackTrace();
67+
}
68+
```
69+
70+
** Publishing to a topic. **
71+
``` java
72+
JSONObject doc = new JSONObject();
73+
try {
74+
filter.put("name", "value");
75+
mPubsub.publish(doc);
76+
} catch (JSONException e) {
77+
e.printStackTrace();
78+
}
79+
```
80+
81+
## Also, make sure to add the following <uses-permission> tags in your manifest file.
82+
83+
* Add a <uses-permission> tag with the INTERNET rule.
84+
* Add a <user-permission> tag with the ACCESS_NETWORK_STATE rule.

bin/AndroidManifest.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
pubsub.io Android Library
4+
5+
Copyright (C) 2011 Andreas Göransson
6+
7+
This program is free software: you can redistribute it and/or modify
8+
it under the terms of the GNU General Public License as published by
9+
the Free Software Foundation, either version 3 of the License, or
10+
(at your option) any later version.
11+
12+
This program is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
GNU General Public License for more details.
16+
17+
You should have received a copy of the GNU General Public License
18+
along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
-->
20+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
21+
package="pubsub.io.android" >
22+
23+
<application >
24+
</application>
25+
26+
</manifest>

bin/jarlist.cache

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# cache for current jar dependecy. DO NOT EDIT.
2+
# format is <lastModified> <length> <SHA-1> <path>
3+
# Encoding is UTF-8

bin/pubsub_android.jar

2.57 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/** Automatically generated file. DO NOT MODIFY */
2+
package pubsub.io.android;
3+
4+
public final class BuildConfig {
5+
public final static boolean DEBUG = true;
6+
}

project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99

1010
android.library=true
1111
# Project target.
12-
target=android-4
12+
target=android-7

0 commit comments

Comments
 (0)