Keep app running

66 views
Skip to first unread message

Peter Bell

unread,
Sep 30, 2019, 7:43:37 AM9/30/19
to CodenameOne Discussions
I am delevoping a marine gps app with testtospeak. I would like to achieve that the app keeps running, also when the screen is off. How do i do that?

Shai Almog

unread,
Sep 30, 2019, 11:51:54 PM9/30/19
to CodenameOne Discussions

Peter Bell

unread,
Oct 1, 2019, 1:30:04 AM10/1/19
to CodenameOne Discussions
Thanx but my problem is not tol het the gps location but to keep the app running for houdt and preventie the system to shut the app down or go to sleep

Shai Almog

unread,
Oct 1, 2019, 11:46:50 PM10/1/19
to CodenameOne Discussions
Did you look at the "Location in the Background" section in the developer guide?

Peter Bell

unread,
Oct 2, 2019, 3:32:45 AM10/2/19
to CodenameOne Discussions
Ues i read the Manuel but i cant figure out of this would-be solve my problem

I am Leyman in making an app

My app muss run for hours and handsfree
So the procedure is that i want to have my location every 30 seconds and a display on my screen
So my app cant be killend nu the systrm
Hoe do i do that
Or is the app not killes because there is every 30
Seconds activity

Shai Almog

unread,
Oct 2, 2019, 11:01:24 PM10/2/19
to CodenameOne Discussions
Background execution is hard core for mobile. Native platforms make it hard on purpose because you're essentially running all the time and "wasting battery" so they don't really want you to do that.

When the app is in the foreground you can get location constantly using the LocationManager (that's the point you display it on the screen). But when the app is minimized the system will kill your app if you keep doing that (as that's taking a lot of battery). So when stop() is invoked in your main class you need to stop polling location. You can then switch to using background location updates using https://www.codenameone.com/javadoc/com/codename1/location/LocationManager.html#setBackgroundLocationListener-java.lang.Class- notice that it works very differently to the regular location listener.
Message has been deleted
Message has been deleted

Peter Bell

unread,
Oct 3, 2019, 4:19:36 PM10/3/19
to CodenameOne Discussions
Thanx Chen

I dont understand completely your solution.

My app  as a routenavigstion app does  four things
1 gets the location
2 display the data
3 move the marker on the map
4 reads out loud the navigation data

I like to keep the app running for hours . I have  enough battery power through connecting the phone to a powerbank.

So i have two issues
1 how to keep the proces of fetching the location alive
2 how do i keep the app  alive to display the data and reads out loud the data.

The first issue could be solved with the backgroundlistner. But can i update my datafield on screen from the backgroundlistner.
In vb net , the background process  are on a different thread. I have to send the data to the other thread. I sthat the same in android.

The second issue i can solve with keeping the screen on. Or starting the app again  when the app is killed.  The problem with that solution is that i need a workarround, because the first screen is a splash screen.

So, how does a route navigation app keeps the app and  the locationmanager awake?



Op donderdag 3 oktober 2019 05:01:24 UTC+2 schreef Shai Almog:

Shai Almog

unread,
Oct 3, 2019, 10:28:48 PM10/3/19
to CodenameOne Discussions
Hi,
the fact that you have enough power doesn't matter. OS's kill processes that take too much CPU without permission because they want to conserve power. That's the OS policy. When you do stuff in the background you can't do anything you want.

If you're running in the foreground you can disable the screensaver using   Display.getInstance().setScreenSaverEnabled(false) which will prevent the device from going to sleep and allow you to see what's going on constantly. This will solve both your problems and won't require any background location logic as your app will be in the foreground unless the user presses the home button or something like that.

Background processes in Android and iOS are far more nuanced and painful. It seems you don't actually need them.

Peter Bell

unread,
Oct 4, 2019, 1:59:40 PM10/4/19
to CodenameOne Discussions
Oke i checked if the app keeps running after hours and it did

The only problem i noticed that the app doesnt update the screen

I change a button from start to stop and stop to start
By stop six labels are also changed
The stop doesnt change to start when i revalidate the button and the form
I dont know what is happeling When running the apps the labels arr changed every 30 seconds


I had the same problem with start but after i changed the initialisation of the locationmanager there was no prblen
Can it be because i set the location manager to null when clicking on the button
Any ideas?

Shai Almog

unread,
Oct 5, 2019, 12:40:22 AM10/5/19
to CodenameOne Discussions
That's unlikely. If you invoke setText() it should implicitly repaint the button, it's possible there was an exception somewhere or some other problem but it's probably unrelated to the screensaver. It might be related to you overriding paint or blocking the EDT somewhere else in the code.

Peter Bell

unread,
Oct 12, 2019, 3:53:42 AM10/12/19
to CodenameOne Discussions
Hi chen, when i use my app for hours then when i change stop to start settext doesnt happen.  Is there a log where i can see what has happend. 
It only happens after hours and the rest of the code is functioning good.

Op zaterdag 5 oktober 2019 06:40:22 UTC+2 schreef Shai Almog:

Shai Almog

unread,
Oct 12, 2019, 10:07:10 PM10/12/19
to CodenameOne Discussions
Everything you write to Log.p/e is logged. There is also native logging. You can use Log.sendLogAync() in the pro accounts you can also use this: https://www.codenameone.com/blog/native-logging-certificate-wizard.html for all account levels.
You can also connect your Android device with a cable and look through logcat output to see if there's a problem you can spot.

Peter Bell

unread,
Oct 14, 2019, 2:00:29 AM10/14/19
to CodenameOne Discussions
Thanx  shai ,
Can i get with logging why the button.settext is not rendering?   Does the system respond to  try and error with settext?

Op zondag 13 oktober 2019 04:07:10 UTC+2 schreef Shai Almog:

Shai Almog

unread,
Oct 14, 2019, 9:48:40 PM10/14/19
to CodenameOne Discussions
Do you mean try/catch?
That should work just log the exception e.g.:

catch(Throwable t) {
   
Log.e(t);
}


instead of 

catch(Throwable t) {}


Reply all
Reply to author
Forward
0 new messages