× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

android application with phonegap that run websocket in background service

Xaver Kapeller
Mar 13, 2015
<p>I think you should use a different approach. What you are trying to do is not really possible on Android. But you can use <a href="http://developer.android.com/google/gcm/index.html" rel="nofollow"><strong>Google Cloud Messaging</strong></a> to push data to devices with your app installed. It kinda works like this:</p> <p><img src="http://i.stack.imgur.com/FaRzP.png" alt="enter image description here"></p> <p>As long as you have some server, for example a <a href="https://developers.google.com/appengine/?csw=1" rel="nofollow"><strong>Google App Engine</strong></a> 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.</p> <hr> <h1>Regarding your question</h1> <blockquote> <p>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: <a href="https://github.com/Red-Folder/bgs-core" rel="nofollow">https://github.com/Red-Folder/bgs-core</a>.</p> </blockquote> <p>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 <code>Service</code> 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.</p> <p>Nevertheless there are very detailed tutorials on how to use the <a href="https://github.com/Red-Folder/bgs-core/wiki/Using-the-MyService-Sample" rel="nofollow"><strong>bgs-core</strong></a> plugin:</p> <ul> <li><a href="https://github.com/Red-Folder/bgs-core/wiki/Using-the-MyService-Sample" rel="nofollow"><strong>Using the MyService Sample</strong></a></li> <li><a href="https://github.com/Red-Folder/bgs-core/wiki/Build-your-own-Background-Service" rel="nofollow"><strong>Build your own Background Service</strong></a></li> <li><a href="https://github.com/Red-Folder/bgs-core/wiki/Build-your-own-plugin" rel="nofollow"><strong>Build your own plugin</strong></a></li> </ul> <p>You can also find a <a href="https://github.com/Red-Folder/bgs-sample" rel="nofollow"><strong>sample project</strong></a> on GitHub.</p> <blockquote> <p>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.</p> </blockquote> <p>Android has no native solution for websockets so I recommend you use a websocket library. You can choose one of those three libraries:</p> <ul> <li><a href="https://github.com/TooTallNate/Java-WebSocket" rel="nofollow"><strong>Java-WebSocket</strong></a></li> <li><a href="https://github.com/codebutler/android-websockets" rel="nofollow"><strong>android-websockets</strong></a></li> <li><a href="https://github.com/tavendo/AutobahnAndroid" rel="nofollow"><strong>AutobahnAndroid</strong></a></li> </ul> <p>In the following examples I will use <a href="https://github.com/tavendo/AutobahnAndroid" rel="nofollow"><strong>AutobahnAndroid</strong></a></p> <hr> <p>First you should go through the <a href="https://github.com/Red-Folder/bgs-core/wiki/Build-your-own-plugin" rel="nofollow"><strong>Build your own plugin</strong></a> 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 <a href="http://stackoverflow.com/a/22455835/2310866"><strong>this answer</strong></a>. It details how to create a basic plugin and should cover most if not all the missing information from the other tutorial.</p> <p>When you are done with creating the plugin it is quite simple to establish a connection with <a href="https://github.com/tavendo/AutobahnAndroid" rel="nofollow"><strong>AutobahnAndroid</strong></a>:</p> <pre><code>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); } </code></pre> <p>You can also send data back like this:</p> <pre><code>connection.sendTextMessage(someMessage); </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/24449444/android%20application%20with%20phonegap%20that%20run%20websocket%20in%20background%20service/24534095">Stack Overflow</a>.</p>
comments powered by Disqus