Red-Folder/Cordova-Plugin-BackgroundService

Feature request: execute doWork not only based on a time, but also on demand.

teusink opened this issue · 13 comments

This to make it possible to bind an listener to the app.

Example:

  • Listen to a shake with the phone.
  • Execute code when shake happened.
  • Listen again for a shake.

Stackoverflow example code: http://stackoverflow.com/questions/2317428/android-i-want-to-shake-it

Is the intention to run code in the background (java) or in the foreground (javascript)?

Intention is to create a listener that runs Java code. Roughly the same as it is now, only without the timer.

Code written. Just need to test and upload. Hopefully get a chance this weekend.

Wow, awesome!

www.teusink.org
www.droidpapers.org
Op 13 sep. 2013 17:55 schreef "Red-Folder" notifications@github.com het
volgende:

Code written. Just need to test and upload. Hopefully get a chance this
weekend.


Reply to this email directly or view it on GitHubhttps://github.com//issues/14#issuecomment-24404419
.

Added runOnce() support this morning. This can be run from Javascript (via the Plugin) or from the Background Service.

The runOnce() will run the doWork() then update the latest result (in the same manner as the timer does). This means that reading the LatestResult within your Javascript will return the result of the runOnce execution (assuming it was the last to run).

Neither the Plugin or Background Service runOnce() accept any parameters - so if you need data to work on in your doWork() then you will need to pass this out-of-band. For the Plugin (from Javascript) I'd recommend sending the data in setConfig. For the Background Service I'd recommend setting a member field/ variable.

I'll leave this issue open until such time as I've had a chance to document an example of its use.

@Red-Folder Nice! I hope I have sunday time to test it. I will add a listener for a shake and upon shake code will be executed. I will let you know what the outcome is. Thanks!

I am testing with it, and pretty much everything seems to work.

I notice that after the service is started, the runOnce is not automatically run. When it comes to biding listeners in the service this is not handy. Because sometimes Android restart services and the listener is gone. I have to manually do a runOnce in order to get things back to working.

Any thoughts on this?

Override protected abstract JSONObject initialiseLatestResult();

Put the listener setup in there. This should be called if Android restarts the service.

Let me know how you get on

That should probably do the trick indeed. I will test that out.

Another question. Since I run the latest version somehow the getStatus doesn't result in a succes callback (or something). I had nested some things like this:

autochanger.getStatus( function (r) { handleAutoChangerSuccess(r); }, function (e) { handleAutoChangerError(e); } );

function handleAutoChangerSuccess(data) { if (!data.ServiceRunning && handleAndroidPreferences("get", window.androidPrefsLib, "settingAutoChanger", "") === "on") { console.info("PhoneGap Plugin: AutoChanger Service: started service, because it should be running."); serviceAutoChanger("startService", "none"); } if ($.mobile.activePage.is('#wallpaperSettingsPage')) { updateView(data); } }

The callback of the getStatus command is the handleAutoChangerSucces part. When the service is not running it should automatically start it.

Edit:
Never mind. It is the "handleAndroidPreferences" part. The callback takes to long or something to properly do the comparison.

Never mind. It is the "handleAndroidPreferences" part. The callback takes to long or something to properly do the comparison.

Please help me, I have a error: Error: Plugin unable to bind to background servace
ErrorCode: -4 ...
I do everything according to instructions.
http://zalivalka.ru/79472
Who has a working source code, send me please danreegly@gmail.com

@vladjiss Please stop adding comments to existing issues. The issue should be kept on topic. Please create a new issue and provide a copy of your code so that I can assist. It will almost certainly be because you have missed a step in the instructions - which I'm prepared to help with, but please stop spamming the issues list

I have now documented the runOnce functions, these can be found in the new repo WIKI:

Now documented I consider this issue closed