Background services

859 views
Skip to first unread message

Honza Jan

unread,
Dec 16, 2016, 9:54:55 AM12/16/16
to DroidScript
Hi!
I want to ask if I can create service wich won't stop when I close the main app.

Steve Garman

unread,
Dec 16, 2016, 11:41:47 PM12/16/16
to DroidScript
There are a couple of samples starting from http://wiki.droidscript.me.uk/doku.php?id=built_in:service

Honza Jan

unread,
Dec 18, 2016, 11:19:26 AM12/18/16
to DroidScript
But when you close the main app it will stop working

Steve Garman

unread,
Dec 18, 2016, 5:31:27 PM12/18/16
to DroidScript
Those samples don't stop working when I close the main app.
Message has been deleted

Honza Jan

unread,
Dec 23, 2016, 7:14:06 PM12/23/16
to DroidScript
Ok. It won't stop running, but the music will stop playing.

Dave Smart

unread,
Dec 24, 2016, 1:32:56 PM12/24/16
to DroidScript
It does not stop playing for me.

What phone are you using?

What version of Android?

How are you closing the app.. are you using the back button?


Honza Jan

unread,
Dec 27, 2016, 2:03:14 PM12/27/16
to DroidScript
I'm using Vodafone SMART 6 Ultra and Android 5.1.1

When I close the app using home/back button only it keeps playing. But when I fully close the app the music stops, but service is still running.

Honza Jan

unread,
Dec 27, 2016, 2:09:40 PM12/27/16
to DroidScript
And when I use autostart it only show popup

Dave Smart

unread,
Dec 29, 2016, 2:50:25 PM12/29/16
to DroidScript
What do you mean by 'fully close the app'  that sounds like you are killing the app task (which is not the proper way to close an app).  You should use either the back button or the app.Exit() method.

If you kill the app using the phone's task manager, then it's associated service might get killed too on some phones.  The service will restart automatically if it is killed but the playback will have stopped, so you would have to start the playback again in your service code.

Honza Jan

unread,
Dec 30, 2016, 4:29:18 PM12/30/16
to DroidScript
Video:



Code:
Main:
function OnStart() {
    app.SetOrientation("Portrait");
    app.ShowProgress("Loading...");
    app.SetTitle("SuperCoolApp V2");
    
    lay = app.CreateLayout("Linear", "VCenter, FillXY");
    
    btn = app.CreateButton("Start", 0.7, -1);
    btn.SetOnTouch(btn_OnTouch);
    
    lay.AddChild(btn);
    
    app.AddLayout(lay);
    
    app.ShowProgress("Creating service...");
    ser = app.CreateService("this", "this", OnServiceReady);
    ser.SetOnMessage(OnServiceMessage);
}
function btn_OnTouch() {
    ser.SendMessage("HEY! Wake up!");
}
function OnServiceReady() {
    app.HideProgress();
}
function OnServiceMessage(msg) {
    console.log("Main: " + msg);
}

Service:
var i;
var p = 0;
function OnStart() {
    i = setInterval(run, 500);
}
function OnMessage(msg) {
    app.ShowPopup("Service: " + msg);
    app.SendMessage("Done!");
}
function run() {
    console.log("I'm still active!" + p);
    app.ShowPopup("I'm still active!" + p);
    p++;
}
function OnResume() {
    i = setInterval(run, 500);
}

Dave Smart

unread,
Dec 31, 2016, 2:02:31 PM12/31/16
to DroidScript
In that video, are you launching an APK or are you just using a shortcut to a DS project?

Dave Smart

unread,
Dec 31, 2016, 2:35:03 PM12/31/16
to DroidScript
OK.  I investigated further and I'm not sure if this is exactly your problem or not, but DS does not currently like it when you kill a service using task manager apps but it is fine when you close an app normally or use app.Exit.

I've now fixed it so that killing the app and service using task managers will allow the service to restart properly (the service was crashing previously).


Message has been deleted
Message has been deleted
Message has been deleted

Honza Jan

unread,
Jan 1, 2017, 4:30:41 PM1/1/17
to DroidScript
It's APK

Ev. rodrigo dos santos

unread,
Jan 1, 2017, 5:54:20 PM1/1/17
to DroidScript
Yes, thank you, now it's complete, working.

Netpower8

unread,
Jan 1, 2017, 7:29:29 PM1/1/17
to DroidScript
@Rodrigo de santos
Your welcome.

@Honza
my previous msg was to solve rodrigo's problem. Regarding background service stopping. Mine works fine. But on one of my unit i have a DS battery save app. It kills the droidscript background music servicr app. So i think there is no problem with droidscript code since it look like most people tried the ds app and works fine. In your case you might have some app that kills the background service. Apps like ds battery saver or greenify or similar apps. Stop these app from running and try the background service of DS again it should work. Or try it on a different android unit.

Netpower8

unread,
Jan 1, 2017, 7:32:45 PM1/1/17
to DroidScript
My mobile unit is android kitkat 4.4. Therr is a possibility that it could be a lollipop issue. But since i dont have a lollipop device i cannot test this.

Honza Jan

unread,
Jan 2, 2017, 9:12:00 AM1/2/17
to DroidScript
I don't have any app like this

Honza Jan

unread,
Jan 2, 2017, 9:13:43 AM1/2/17
to DroidScript
@Dave Smart
So in new version of DS it won't stop working?

Dave Smart

unread,
Jan 2, 2017, 12:08:43 PM1/2/17
to DroidScript
In the video it looks like you are killing apps using the task manager. It may depend on your phone model whether the service is also killed, but if the service is killed, then it will re-start again with the new version.

Jon Bray

unread,
Jan 28, 2020, 2:02:30 PM1/28/20
to DroidScript
I can't see any mention of foreground services anywhere (A relatively new addition to Android I think - requires apps to have a notification if they're going to stay on all the time).  - this thread is quite old so it may predate the change.  Does DroidScript still work the same way (ie permanently running service using the above code)?  I need to be able to check a Puck every 10 minutes whether the screen is on or not.

Steve Garman

unread,
Jan 28, 2020, 4:55:23 PM1/28/20
to DroidScript
If you have a look in the built-in docs at "CreateService" you will notice the service has a method SetInForeground.

This should be placed in your Service.js OnStart function to ensure it is called within a few seconds of startup. That is an Android requirement to prevent it reverting to a background service.

You can also call it as many times as you like to change the notification message.

The docs can also be seen at
https://alex-symbroson.github.io/Docs/docs/app/CreateService.htm#Methods

If you search this group for
"SetInForeground" you will probably find some helpful discussion.

Jon Bray

unread,
Jan 31, 2020, 11:32:34 AM1/31/20
to DroidScript
OK, I've tried doing that.  This is the first bit of my Service.js:

 _AddPermissions("Bluetooth");
  
var buttonDelay=500;    //The delay in millisecs before a new button press is defined.
var lastPress=0;        //Time of last press
var thisPress=0;        //Time of last press
var numPresses=0;       //Number of times the button has been pressed this press


//Init variables.
var count = 0;
var diff = 1;


function OnStart(){
    
  app.SetInForeground("PuckJon","Connecting...","","","low");

-----------

However I'm getting the following error:

svc: WARNING: App.SetInForeground() failed! (Attempt to invoke virtual method 'void android.graphics.Bitmap.setDensity(int)' on a null object reference)

Any ideas please?





Steve Garman

unread,
Jan 31, 2020, 1:41:09 PM1/31/20
to DroidScript
I've never used empty strings like that in SetInForeground

Try changing both instances of "" with the word null (not in quotes)

I can't guarantee that's the problem but I definitely recommend making the change anyway.

Jon Bray

unread,
Feb 1, 2020, 12:22:20 PM2/1/20
to DroidScript
Yes, that seems to work fine thanks.

I think I had tried it with just a row of commas (which I thought was the same as null) and that didn't work, so I remain somewhat confused, but glad you could translate that error message!

GOOD

unread,
Jun 2, 2020, 12:18:07 AM6/2/20
to DroidScript
Can I use modern code? Launch applications. Closing the app in any way (even restarting the device) and then the service starts working(like Google play market)
Reply all
Reply to author
Forward
0 new messages