Reputation: 584
I am new to Android but familiar with web programming. Currently I am using phonegap to write an app.
My application receives news via websockets and displays them to the user.
My problem is that when my application is closed by user, I cannot use the WebView
for receiving news. After searching a while I found a plugin for Phonegap that can run background services with Java: https://github.com/Red-Folder/bgs-core.
But I'm new to Java and I don't know how to run websockets (autoban.ws for Android) in a background service to receive news and show it in the notification bar.
Upvotes: 0
Views: 4132
Reputation: 1025
It's very simple,according to documentation of https://github.com/Red-Folder/bgs-core/wiki/Using-the-MyService-Sample:
The class provides the background service logic in the doWork. Notice that the doWork is not only performing the logic required (in this case it simply prints a Hello World message to the logcat) it also produces a result JSONObject. This result is made available to the HTML/ Javascript Front-End through the Returned JSON:
@Override
protected JSONObject doWork() {
JSONObject result = new JSONObject();
try {
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String now = df.format(new Date(System.currentTimeMillis()));
String msg = "Hello " + this.mHelloTo + " - its currently " + now;
result.put("Message", msg);
Log.d(TAG, msg);
} catch (JSONException e) {
}
return result;
And according to this link: http://autobahn.ws/android/gettingstarted.html
package de.tavendo.test1;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import de.tavendo.autobahn.WebSocketConnection;
import de.tavendo.autobahn.WebSocketException;
import de.tavendo.autobahn.WebSocketHandler;
public class Test1Activity extends Activity {
private static final String TAG = "de.tavendo.test1";
private final WebSocketConnection mConnection = new WebSocketConnection();
private void start() {
final String wsuri = "ws://192.168.1.132:9000";
try {
mConnection.connect(wsuri, new WebSocketHandler() {
@Override
public void onOpen() {
Log.d(TAG, "Status: Connected to " + wsuri);
mConnection.sendTextMessage("Hello, world!");
}
@Override
public void onTextMessage(String payload) {
Log.d(TAG, "Got echo: " + payload);
}
@Override
public void onClose(int code, String reason) {
Log.d(TAG, "Connection lost.");
}
});
} catch (WebSocketException e) {
Log.d(TAG, e.toString());
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start();
}
}
Finally combine these example you have your own goal(run WebSocket in background service):
@Override
protected JSONObject doWork() {
try {
final String wsuri = "ws://echo.websocket.org/";
try {
mConnection.connect(wsuri, new WebSocketHandler() {
@Override
public void onOpen() {
Log.d(TAG, "Status: Connected to " + wsuri);
mConnection.sendTextMessage("Hello, world!");
}
@Override
public void onTextMessage(String payload) {
Log.d(TAG, "Got echo: " + payload);
try {
result.put("wsMessage",payload);
} catch (JSONException e) {
}
}
@Override
public void onClose(int code, String reason) {
Log.d(TAG, "Connection lost.");
}
});
} catch (WebSocketException e) {
Log.d(TAG, e.toString());
}
} catch (JSONException e) {
}
Log.d(TAG, result.toString());
return result;
}
Upvotes: 0
Reputation: 49837
I think you should use a different approach. What you are trying to do is not really possible on Android. But you can use Google Cloud Messaging to push data to devices with your app installed. It kinda works like this:
As long as you have some server, for example a Google App Engine project, you can push any data to your apps and you can also target specific devices. Most apps use Google Cloud Messaging as it is very efficient and battery friendly but also very fast. Without Google Cloud Messaging or something similar you would have to poll the server periodically and check for updates. This wakes the device up and drains the battery - especially when you need frequent updates. Google Cloud Messaging solves all of those problems, but it is also going to be a little more work if you have never done anything like that before. But since you are a web developer I think you should be able to handle it.
My problem is that when my application is closed by user, I can't use webview for receiving news. After searching I found a plugin for Phonegap that can run background services with Java: https://github.com/Red-Folder/bgs-core.
Generally this is kind of a bad idea. And it won't work on Android. You cannot have anything that runs in the background permanently. Even if you could, such a Service
would drain the battery very fast as the device could never sleep. Even if you were just polling as I described above you would still have to wake the device up every x minutes and check for updates. So I can just reiterate: Use Google Cloud Messaging.
Nevertheless there are very detailed tutorials on how to use the bgs-core plugin:
You can also find a sample project on GitHub.
But I'm new to Java and don't know how to run websockets (autoban.ws for Android) in a background service to receive news and show it in the notification bar.
Android has no native solution for websockets so I recommend you use a websocket library. You can choose one of those three libraries:
In the following examples I will use AutobahnAndroid
First you should go through the Build your own plugin tutorial! This tutorial assumes that you already know the basics of how to create plugin, but since you are relatively new to all of this you can take a look at this answer. It details how to create a basic plugin and should cover most if not all the missing information from the other tutorial.
When you are done with creating the plugin it is quite simple to establish a connection with AutobahnAndroid:
final WebSocketConnection connection = new WebSocketConnection();
try {
connection.connect(url, new WebSocketHandler() {
@Override
public void onOpen() {
// Web socket connection has been opened
}
@Override
public void onTextMessage(String payload) {
// Received text message
}
@Override
public void onClose(int code, String reason) {
// Web socket connection was closed
}
});
} catch (WebSocketException e) {
Log.d(LOG_TAG, "Could not connect!", e);
}
You can also send data back like this:
connection.sendTextMessage(someMessage);
Upvotes: 5