Android Question Background app

KarlB

Member
I have a project that should run continuously in the background to save energy. It is probably similar to the MyLocation example but that example is not commented and does nothing on my phone except show a title.

My application needs to wake up every 10 minutes, make a Bluetooth connection, read some serial data, and send it to ThingSpeak. No user interaction is needed so the screen can stay dark.

I am a complete newb to B4X so please help me understand what I need to do to make this work. I have attached my code that works very well as a foreground app.

I also attached a screenshot from the MyLocation example that doesn't look anything like the screenshot included in the MyLocation article. My phone is a Samsung S21 running Android 11.
 

Attachments

  • Screenshot_20210527-131334.jpg
    51.5 KB · Views: 108
  • BridgeHandShake.zip
    23.3 KB · Views: 98

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is probably similar to the MyLocation example but that example is not commented and does nothing on my phone except show a title.
The example updates the notification with your current location. The location related code is less important. You need to learn that example to understand how to create a foreground service that lets your app keep running in the background.

Another option is to use StartServiceAtExact to schedule the app to start in 10+ minutes from now.
 
Upvote 0

KarlB

Member
I don't see what Starter does. Where is the background service: Main or Tracker? Likewise, where is the foreground service?

How does the DateTime in GPS_locationChanged get called if the location does not change?
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I don't see what Starter does. Where is the background service: Main or Tracker? Likewise, where is the foreground service?
Search the code for Service.StartForeground

How does the DateTime in GPS_locationChanged get called if the location does not change?
I don't remember the code and I don't understand the question.

Also, what permissions does my app need? How can I tell?
No special permission is needed to run in the background. You do need to handle the Bluetooth permissions.


What would the service type be for my app?
You don't need this line.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…