java - android application with phonegap that run websocket in background service -
i new android familiar web programming. using phonegap write app.
my application receives news via websockets , displays them user.
my problem when application closed user, cannot use webview
receiving news. after searching while found plugin phonegap can run background services java: https://github.com/red-folder/bgs-core.
but i'm new java , don't know how run websockets (autoban.ws android) in background service receive news , show in notification bar.
i think should use different approach. trying not possible on android. can use google cloud messaging push data devices app installed. kinda works this:
as long have server, example google app engine project, can push data apps , can target specific devices. apps use google cloud messaging efficient , battery friendly fast. without google cloud messaging or similar have poll server periodically , check updates. wakes device , drains battery - when need frequent updates. google cloud messaging solves of problems, going little more work if have never done before. since web developer think should able handle it.
regarding question
my problem when application closed user, can't use webview receiving news. after searching found plugin phonegap can run background services java: https://github.com/red-folder/bgs-core.
generally kind of bad idea. , won't work on android. cannot have runs in background permanently. if could, such service
drain battery fast device never sleep. if polling described above still have wake device every x minutes , check updates. can reiterate: use google cloud messaging.
nevertheless there detailed tutorials on how use bgs-core plugin:
you can find sample project on github.
but i'm new java , don't know how run websockets (autoban.ws android) in background service receive news , show in notification bar.
android has no native solution websockets recommend use websocket library. can choose 1 of 3 libraries:
in following examples use autobahnandroid
first should go through build own plugin tutorial! tutorial assumes know basics of how create plugin, since relatively new of can take @ this answer. details how create basic plugin , should cover if not missing information other tutorial.
when done creating plugin quite simple establish connection 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 closed } }); } catch (websocketexception e) { log.d(log_tag, "could not connect!", e); }
you can send data this:
connection.sendtextmessage(somemessage);
Comments
Post a Comment