Location service is not working in background

243 views
Skip to first unread message

MrPDaddy

unread,
Sep 21, 2016, 4:51:57 AM9/21/16
to DroidScript
Can anybody help me to fix this code, I want to run this code in background, but unfortunetly it is not running , I set two min timmer and according to code it should show complete log which run in background but it starts from begining .

if it is working fine in your phone please let me know.
Thank
//Called when application is started.
function OnStart()
{
   
//Create a layout with objects vertically centered.
    lay
= app.CreateLayout( "linear", "VCenter,FillXY" );    
   

   
   
//Create a text control to show data log.
    txt
= app.CreateText( "", 0.9, 0.8, "Multiline,Left" );
    txt
.SetTextColor( "#ffffffff" );
    lay
.AddChild( txt );
   
       
//Create a 'Set Alarm' button.
    btnSet
= app.CreateButton( "Set Alarm", 0.4, 0.1 );
    btnSet
.SetMargins( 0, 0.05, 0, 0 );
    btnSet
.SetOnTouch( btnSet_OnTouch );
    lay
.AddChild( btnSet );
   
   
//Create and start location sensor.
   
//(Achievable update rate is hardware specific)
    loc
= app.CreateLocator( "GPS,Network" );
    loc
.SetOnChange( loc_OnChange );
    loc
.SetRate( 10 ); //10 seconds.
    loc
.Start();
   
   
//Add main layout to app.
    app
.AddLayout( lay );
   
   
//Create array to hold log messages.
    log
= new Array();
   
Log( "Locating..." );
   
   
//Add layout to app.    
    app
.AddLayout( lay );
   
   
//Create media player and load file.
    player
= app.CreateMediaPlayer();
    player
.SetFile( "/Sys/Snd/Poing.ogg" );
}

//Called when user touches our 'Set Alarm' button.
function btnSet_OnTouch()
{
   
//Get current time in milliseconds.
   
var now = new Date().getTime();
   
   
//Set alarm for 3 seconds time.
    app
.SetAlarm( "Set", 1234, OnAlarm, now + 120000);
    app
.ToBack();
}

//Called when alarm is triggered.
//(Even if your app is closed)
function OnAlarm( id )
{
    app
.ShowPopup( "Got Alarm: id = " + id );
    player
.SeekTo( 0 );
    player
.Play();
}


//Called when we get a change in location.
function loc_OnChange( data )
{

//btnSet_OnTouch();
   
Log( data.provider+": Lat "+data.latitude+", Lng "+data.longitude
       
+", Spd "+data.speed+", Bear "+data.bearing
       
+", Alt "+data.altitude+", Accu "+data.accuracy+"" );
       
   
}

//Add messages to log.
function Log( msg )
{
   
if( txt.GetLineTop( txt.GetLineCount() ) >= 0.8 )
        log
.shift();
    log
.push( msg + "\n" );
    txt
.SetText( log.join("") );
}

Dave Smart

unread,
Oct 4, 2016, 8:09:30 AM10/4/16
to DroidScript
Hi,

You need to use a DroidScript service to run things in the background.

(There is a sample music service somewhere on this forum)

Ray Perkins

unread,
Feb 19, 2017, 9:20:22 AM2/19/17
to DroidScript
I have the same issue. I can successfully start and stop location services (loc.Start(); etc.) but I can't do it while in the background. Did you find a way to do it using the service function? I can't figure out how to use it for location. Thanks for any help.

Steve Garman

unread,
Feb 19, 2017, 10:25:30 AM2/19/17
to DroidScript
Here is a minimal service that starts and stops a locator.
locateService.spk

Ray Perkins

unread,
Feb 21, 2017, 11:26:47 AM2/21/17
to DroidScript
Thanks, Steve. It's getting clearer! I just have to figure out how to pass the lat and long variables back to the main app.

Steve Garman

unread,
Feb 21, 2017, 12:47:19 PM2/21/17
to DroidScript
You can use app.SendMessage to send a string back to the main app.

The attached spk uses JSON.stringify to send the whole data object as a string and Json.parse to turn it back into an object at the other end,
locateService2.spk

Ray Perkins

unread,
Feb 21, 2017, 2:09:29 PM2/21/17
to DroidScript
Thanks once again, Steve. I found the app.SendMessage function, but didn't know how to retrieve the data in the main app. I have been trying to write a tracking app that responds with a location upon receipt of a coded SMS message, but I was having trouble getting the gps to work in the background. With your help, I think I can do it!
Message has been deleted
Message has been deleted

Ray Perkins

unread,
Feb 24, 2017, 9:02:09 AM2/24/17
to DroidScript
Tracking app: Steve (and whoever else): Not asking for help, just posting my app files in case someone finds them useful. Tried to run location as a service so I could turn it on and off while in the background (to save battery), but it only works sometime. Pretty much given up trying to do it. Other than that, works fine! Thanks again for the advice. Posted the *.js files as txt as I didn't know how to attach them correctly.
Service.txt
SMStracker.txt

Pete Bradley

unread,
Sep 18, 2018, 7:16:53 PM9/18/18
to DroidScript
is this feature broken in newer versions of Android?   I downloaded the SPK and built an APK and intalled the app, it works when the app is active, but when I switch to another app the vibration stops..  which is telling me the background service is no longer doing anything.

Is there anyway around this?  Is android killing off services if it thinks its low on resources? .

Pete Bradley

unread,
Sep 18, 2018, 7:21:38 PM9/18/18
to DroidScript
My service was working then it stopped working and that is when I tried this to see if I changed something in my code..   Now the only thing I can think of that I changed recently was upgrading Droid script and the APK builder to get rid of the target API msgs.   Maybe it's the newer android API that doesn't like services.

Ray Perkins

unread,
Sep 18, 2018, 9:28:54 PM9/18/18
to DroidScript
In case it might shed some light... I wrote a test program that runs as a service and writes a timestamp once a minute to a text file (running from a 10 second interrupt in the routine). Fromm looking at the file, it appears that when Android is in sleep mode, it gradually puts the processor in a dormant state, checking for any requests every few minutes, dropping down to maybe once every 20 minutes after about half an hour. This was really messing up my program, as I wanted to check the gps every minute, so I now don't use sleep mode. This shutting down of the processor in sleep mode is understandable from a battery perspective, and may well vary with different versions of the OS or manufacturer.
I found something online that agrees with my observations.

Pete Bradley

unread,
Sep 19, 2018, 11:02:27 AM9/19/18
to DroidScript
I disabled battery optimization for my app android still stops it when the app is in the background.   This is happening even when the device is not asleep, if I switch to another app my background service stops.   If I switch back to my app the background service continues...   I only have one android pie device so I can't test on anything else.

Pete Bradley

unread,
Sep 19, 2018, 11:14:36 AM9/19/18
to DroidScript
looks like we are out of luck with background services now, Android has killed their usability with Android 8+   ..  The only workaround is JobScheduler, but I don't think it's implemented into Droidscript yet, looks like someone has already added it to the V2 request.   

Steve Garman

unread,
Sep 19, 2018, 11:44:27 AM9/19/18
to DroidScript
DroidScript has not really been comprehensively tested past Oreo

I must admit I was under a misunderstanding about the services problem.

I thought that while we were targetting api 26 we were safe from that but apparently that is the cutoff point.

This may well not help but does your Service.js contain a SetAlarm?

If not, I would recommend try adding this code in the service OnStart function

var now = new Date().getTime();

app.SetAlarm( "Repeat", 1324, null, now + 3000, 3000 );

I would be interested to hear if it helps.

The only other "fix" I can suggest, if you don't need to post your app on Google Play is a build.json file with a targeSdkVersion lower than 26.

To be honest, that hasn't been tested much with the most recent version of DroidScript either.

Pete Bradley

unread,
Sep 19, 2018, 12:56:13 PM9/19/18
to DroidScript
that unfortunately didn't help.  

Anyone skilled with plugins that can add ability to use JobScheduler?
Message has been deleted

Pete Bradley

unread,
Sep 19, 2018, 3:42:21 PM9/19/18
to DroidScript
I tried changing the target api and I am still having issues with the background service staying active.


Pete Bradley

unread,
Oct 1, 2018, 2:36:58 PM10/1/18
to DroidScript
Steve, do you know if it's possible to add jobscheduler with a plug-in? I thought about diving into it myself with your plug-in guide, but don't want to waste my time if it's not even possible.

Steve Garman

unread,
Oct 1, 2018, 2:42:44 PM10/1/18
to DroidScript
I don't know for sure if it's possible.

I'm hoping foreground services may be available in the next alpha. It may be that will help your problems.

Jari Ruokolainen

unread,
Dec 11, 2025, 10:00:52 AM (3 days ago) Dec 11
to DroidScript
Hi! Any development with this? I notice this sensor background problem with other sensors, such as the acc. I use DS 2.78.9 and Galaxy A51 5G. If no solution comes, I feel a need to switch to an Android version that can be versioned.
Reply all
Reply to author
Forward
0 new messages