android: how to make sure my app doesnt get paused when running in the background

mobileDev

Android app that I am working on reads from near by beacons(devices) using bluetooth. It works fine when the app is in the foreground (tested it for 20 minutes). However, few minutes after app goes to background it stops reading.

I notice when app goes to background, onpause() method is executed; still my app reads for few minutes and then simply stops reading anything (when I manually bring the app to foreground, oncreate method is executed and app continuous normally).

Why is my app stopped reading few minutes after it went to background. My app is an activity and not service.

  1. should convert the activity to service or
  2. should I create intentservice or
  3. should I create foregroundserive

I donot understand the difference between above 3 types of services and if any of them would help me.

Though slightly older threads, I reviewed Prevent that the app get stopped or paused by the OS and How can we prevent a Service from being killed by OS? and my app killed by android system when it running in background

But I am lost. Any discussion is appreciated

EDIT

As I understand from @davidgyoung answer, I have to write a service. I assume GUI portion of my app goes into mainactivity; then how I can ensure my mainactivity/GUI is still active in memory and was not killed by Android by the time service tries to broadcast/notify GUI

/EDIT

davidgyoung

An Activity is not designed to run for long periods in the background. The Android OS will destroy activities that are not visible as memory is needed for other functions. While a Service is the proper alternative, even a service will be destroyed under memory pressure by the OS, so you still need to restart the service if it is killed by the OS and you continue to want to do beacon scanning.

All of these issues came up when we built the Android Beacon Library, and we settled on these solutions to keep scanning going:

  • Use a Service to scan for beacons in the background. It does not have to be an IntentService, but that is a reasonable option.

  • Use an AlarmManager to restart the scanning service 5 minutes in the future in case it gets killed. (This delay allows the OS to time to recover from a temporary need for extra memory.) If the scanning service is still running, just reschedule the alarm.

  • Register for OS level events (boot, power connect/disconnect) to restart the scanning service at a later time if the user kills the app with the task switcher.

All of this is built for you if you decide to use the Android Beacon Library (and we welcome contributions, too!) If you want to roll your own, you may want to look at the source code to see how these things were built. Feel free to copy and modify, too. That's the beauty of open source!

Full disclosure: I am the lead developer on the Android Beacon Library open source project.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

android: how to make sure my app doesnt get paused when running in the background

From Dev

iOS Enterprise app - How can I make sure my app runs in the background?

From Dev

Android App Crashes when paused

From Dev

Android App Crashes when paused

From Dev

How to make sure that rails server is running in background on a vps

From Dev

Parse CorodvaPush Ionic: Android doesnt show notifications when app in background

From Dev

How to make sure app restarts each time its opened even if its just put in background? android

From Dev

How do I make sure my script is running on Open Shift?

From Dev

How to know Package Name of a Top running app from my service running in background (android)

From Dev

Making sure android app is running

From Dev

How can I make sure a program is killed when VPN is running?

From Dev

how to make sure nginx is running

From Dev

how to make sure nginx is running

From Dev

How to make background of my app white?

From Dev

How to make sure my PHP app wont be distibuted?

From Dev

Why doesnt my app make phonecall?

From Dev

Can i make call to webservice when App is running in background

From Dev

Android 5.1 make background running APP slow and timer imprecise

From Dev

How to get names of background running apps in android?

From Dev

Landscape crashes when paused and Portrait doesnt

From Dev

How to make an android app to always run in background?

From Dev

How to make title background transparent on android app

From Dev

How to get notification of mouse and keyboard use in Mac when app is running in background?

From Dev

How to get notification of mouse and keyboard use in Mac when app is running in background?

From Dev

Are countdowntimers stopped/destroyed when my App is paused/killed/destroyed?

From Dev

How can i make my app when running it to start clean on every launch?

From Dev

How to know my android app is running in the receiver?

From Dev

How to change the button background image when music is paused

From Dev

Does NSURLSessionDataTask get paused on background?

Related Related

  1. 1

    android: how to make sure my app doesnt get paused when running in the background

  2. 2

    iOS Enterprise app - How can I make sure my app runs in the background?

  3. 3

    Android App Crashes when paused

  4. 4

    Android App Crashes when paused

  5. 5

    How to make sure that rails server is running in background on a vps

  6. 6

    Parse CorodvaPush Ionic: Android doesnt show notifications when app in background

  7. 7

    How to make sure app restarts each time its opened even if its just put in background? android

  8. 8

    How do I make sure my script is running on Open Shift?

  9. 9

    How to know Package Name of a Top running app from my service running in background (android)

  10. 10

    Making sure android app is running

  11. 11

    How can I make sure a program is killed when VPN is running?

  12. 12

    how to make sure nginx is running

  13. 13

    how to make sure nginx is running

  14. 14

    How to make background of my app white?

  15. 15

    How to make sure my PHP app wont be distibuted?

  16. 16

    Why doesnt my app make phonecall?

  17. 17

    Can i make call to webservice when App is running in background

  18. 18

    Android 5.1 make background running APP slow and timer imprecise

  19. 19

    How to get names of background running apps in android?

  20. 20

    Landscape crashes when paused and Portrait doesnt

  21. 21

    How to make an android app to always run in background?

  22. 22

    How to make title background transparent on android app

  23. 23

    How to get notification of mouse and keyboard use in Mac when app is running in background?

  24. 24

    How to get notification of mouse and keyboard use in Mac when app is running in background?

  25. 25

    Are countdowntimers stopped/destroyed when my App is paused/killed/destroyed?

  26. 26

    How can i make my app when running it to start clean on every launch?

  27. 27

    How to know my android app is running in the receiver?

  28. 28

    How to change the button background image when music is paused

  29. 29

    Does NSURLSessionDataTask get paused on background?

HotTag

Archive