Hi I'm writing an android app which has to retrieve some infos from a website using webview and js, I figured out how to do that when app is opened, is there a way to get these infos when app is closed? Almost like whatsapp and gmail do...
You're not going to be able to use a WebView when the app isn't showing. What you can do is use a service, which runs in the background, there you can make requests to download data over the Internet.
More info
http://developer.android.com/guide/components/services.html
Keep in mind that your standard service will run on your applications main thread, so you may need to provide threading for yourself.
To make your code trigger at a given time checkout alarms:
http://www.vogella.com/tutorials/AndroidTaskScheduling/article.html
For non WebView web browsing consider:
Use V8 JavaScript engine to execute JS lib without webview
You will need to run your logic on a service.
The service will run on the background and do your app logic.
If you app is doing CPU intensive operations, run your service on another thread. Read the official documentation of services for more information on how to use them.
Related
My android application communicate with web services to download and upload data using AsyncTask. If i receive phone call during communication the application hanged up, else every thing is ok. how i can handle this issue.
Thanks
Try to use a service for these kinds of tasks, for fully understanding what happens it is better to take a look at Android's activity life-cycle:
http://developer.android.com/training/basics/activity-lifecycle/
I have been working on an Android app using Phonegap and now would like to make it so when the app is closed it can still execute the java/js code in the app. So I understand I need to create a service. If I create a service plugin on phonegap can I still execute the javascript code or only the java?
Has anyone does something like this? I found this discussion but did not seem to work: http://groups.google.com/group/phonegap/browse_thread/thread/722b0e796baa7fc6
So that is all I have right now.
Before I turn to developing it native if I can't figure it out thought I would ask if anyone has done this before. I can't seem to find any of the phonegap plugins that do something similar.
EDIT: I have got an app that executes Java code as a service. However when it calls sendjavascript it does not work. So is there a way to have the javascript code running in the background as well when an app is closed with phonegap?
Thanks
No, it is not possible to run Javascript code in the background (at least in my opinion) as a service. Phonegap on Android uses an special activity called Droidgap, which hosts a WebView. This browser control executes the JavaScript. This means that JS execution can only handled inside this activity, regardless if it is visible or not.
The code you linked from Google Groups tries to bind a service developed in Java to the DroidGap activity, so the service is NOT written in JS.
You can have some background activity within your JS code inside your child activity derived from the DroidGap activity. For example have a background thread in your activity, have a JS callback function and let the thread call this callback functionality.
If you really need a service you have to go native.
Update:
JS code can only be executed with the Droidgap activity. An activity can have 3 states (based on the Lifecycle of activites):
visible
invisible but still loaded
not loaded
I provided a sample in which I implemented a Phonegap plugin. The plugin allows the activity to register itself to SMS_RECEIVED. When the activies goes out of scope (event onbeforeunload), it deregisters, so only issue 1 is handled.
When you want all 3 issues handled, you have to forward the incoming SMS intent to the activity. When it is not loaded the system will automatically load and activate the activity. But this is not a background service anymore, your app would become visible whenever a SMS is received.
If you don't want this (if you really want a background service), you have to provide a native implementation.
There is this article on how to create a service on Android with Phonegap which gives some good information on your problem.
It's using a great plugin in order to build a background service with phonegap easily. But you can't use JS though
I didn't find a way to make JS to run in the Background. BUT you can pass parameters from Java to JS and vice versa with the plugin...which is pretty useful.
You would still need to rewrite your JS code in Java though.
Unless you do have a specific reason to only want JS to be run? (But there shouldn't be...)
Hope that could be useful to some people visiting this page.
YES, and it is very simple... just install the plugin backgroundJS:
https://build.phonegap.com/plugins/430
It allows you to run javascript on the background and combined with the local notification plugin, you can even send notifications to the user at any time, just keep in mind that doing this will cause the battery to run out faster, also consider that this might create a problem with the iOS policy. good luck!!!
You can try to add plugin cordova-plugin-background-mode
But as author says:
Infinite background tasks are not official supported on most mobile operation systems and thus not compliant with public store vendors. A successful submssion isn't garanteed.
Use the plugin by your own risk!
I am trying to run a snippet of javascript which keeps polling the server for updates. what is the best way to get this working with minimal impact on application performance from the end user perspective.
I tried spawning it off in a separate iframe but the resultant performance was pretty bad.
I am currently working on android so if the solution involves writing a phonegap plugin to poll the server instead of javascript that should be fine for now. I need to update data in local storage after polling the server.
I have implemented this in IOS. The problem with phonegap is that you can't create a timer task that runs javascript because if your .html page changes the task would be killed.
Unless you just have ONE .html and you are dynamically changing the body part. If that is the case then look up setInterval in javascript.
If that isn't the case then implementing something on javascript isn't a solution, in IOS I have implemented a custom plugin for running asynchronous web service calls. You could do something similar, writing a plugin that runs a AsyncTask doInBackground() and then postback the results in the javascript. There are many tutorials on how to create a custom plugin in phonegap. Hope it helps
If you are planning to poll only when the application is in use the setInterval() works fairly well. My application is using jQuery Mobile which by default only updates DOM rather reloading entire page, that way my setInterval() keeps running once started.
Android already has a support of Background services, which you can use for the long and continuous running tasks.
One solution which I have used with above combination:
when the app is running I use setInterval() to check for update periodically.
Background checks whether application is running or not before starting the update checks
If the app is not running then I use the background service code just to check whether there are any updates on server or not
If there are updates then I put a system tray notification to notify user that there are updates waiting for him
As soon as he starts the application, I call the service to fetch the updates.
This way my main logic remains in the Javascript side and I need not to write same logic in both Javascript and native again.
If updates are not very frequent then you should consider using PushNotification
I have been working on an Android app using Phonegap and now would like to make it so when the app is closed it can still execute the java/js code in the app. So I understand I need to create a service. If I create a service plugin on phonegap can I still execute the javascript code or only the java?
Has anyone does something like this? I found this discussion but did not seem to work: http://groups.google.com/group/phonegap/browse_thread/thread/722b0e796baa7fc6
So that is all I have right now.
Before I turn to developing it native if I can't figure it out thought I would ask if anyone has done this before. I can't seem to find any of the phonegap plugins that do something similar.
EDIT: I have got an app that executes Java code as a service. However when it calls sendjavascript it does not work. So is there a way to have the javascript code running in the background as well when an app is closed with phonegap?
Thanks
No, it is not possible to run Javascript code in the background (at least in my opinion) as a service. Phonegap on Android uses an special activity called Droidgap, which hosts a WebView. This browser control executes the JavaScript. This means that JS execution can only handled inside this activity, regardless if it is visible or not.
The code you linked from Google Groups tries to bind a service developed in Java to the DroidGap activity, so the service is NOT written in JS.
You can have some background activity within your JS code inside your child activity derived from the DroidGap activity. For example have a background thread in your activity, have a JS callback function and let the thread call this callback functionality.
If you really need a service you have to go native.
Update:
JS code can only be executed with the Droidgap activity. An activity can have 3 states (based on the Lifecycle of activites):
visible
invisible but still loaded
not loaded
I provided a sample in which I implemented a Phonegap plugin. The plugin allows the activity to register itself to SMS_RECEIVED. When the activies goes out of scope (event onbeforeunload), it deregisters, so only issue 1 is handled.
When you want all 3 issues handled, you have to forward the incoming SMS intent to the activity. When it is not loaded the system will automatically load and activate the activity. But this is not a background service anymore, your app would become visible whenever a SMS is received.
If you don't want this (if you really want a background service), you have to provide a native implementation.
There is this article on how to create a service on Android with Phonegap which gives some good information on your problem.
It's using a great plugin in order to build a background service with phonegap easily. But you can't use JS though
I didn't find a way to make JS to run in the Background. BUT you can pass parameters from Java to JS and vice versa with the plugin...which is pretty useful.
You would still need to rewrite your JS code in Java though.
Unless you do have a specific reason to only want JS to be run? (But there shouldn't be...)
Hope that could be useful to some people visiting this page.
YES, and it is very simple... just install the plugin backgroundJS:
https://build.phonegap.com/plugins/430
It allows you to run javascript on the background and combined with the local notification plugin, you can even send notifications to the user at any time, just keep in mind that doing this will cause the battery to run out faster, also consider that this might create a problem with the iOS policy. good luck!!!
You can try to add plugin cordova-plugin-background-mode
But as author says:
Infinite background tasks are not official supported on most mobile operation systems and thus not compliant with public store vendors. A successful submssion isn't garanteed.
Use the plugin by your own risk!
I´m working on a portability of a human-machine dialogue open source system JVoiceXML from Java to Android. The system is based on the VoiceXML 2.0 standard which uses XML documents to create the interactions between the human and the machine.
The idea is to provide Android with a means to creat apps that interact with the user via voice using the TTS and SpeechRecognizer android engines.
We´re trying to do so without having any visual effect on the screen, just as a system service would work. For now I´ve tried to implement our system as a service, but I´m having trouble with threading and Android killing my service.
The question is, which approach do you think fits best my proyect, an AsyncTask, a service, a Loader? Maybe a service that starts an AsyncTask or a Loader?
The requirements would be:
My VoiceXML Interpreter gets fired up by an app Intent (intent filter is how I´m having it now)
It recieves a document from the app. Also the app can stop my Interpreter , or at least stop the interpreting of that document.
The Interpreter interacts with the TTS and creates a Speech Recognizer engine.
The Interpreter itself is a java thread that has to be started from the service, loader, or asyncTask.
Only one app can make use of my Interpreter (no need for parallel connections)
Please let me know if more info is needed to comprehend the problem.
thanks in advance,
Marcos.
If the bulk of your work is already done within another thread, I don't see a need for an AsyncTask. Just use the interpreter from your Service. You'll need a Service no matter what if you want to provide something without a user interface.