I have a problem understanding how the 'Restrict background data' setting in Android really works. I always thought that with this restriction turned on, only visible applications could download data, but currently I'm developing an application that has shaken this understanding... My application registers an alarm in AlarmService to start a background IntentService every 5 minutes. This service will then download the latest financial quotes. What is surprising to me, is that no matter what the 'Restrict background data' setting is, the service is able to download the data over WIFI (didn't try with cellular interface yet).
So my question is, how does 'Restrict background data' setting in Android really work? Also should I worry about it in the case of my application and are there any differences in the behaviour of this option depending on the version of Android (I've checked on 2.3.3 Samsung Galaxy SII)?
how does 'Restrict background data' setting in Android really work?
For Android 1.x/2.x/3.x, it was an "opt-in" setting for developers. Developers should check to see if background data is disallowed and not do it in the background if it is.
For Android 4.0+, Android basically blocks you from doing Internet access if the user's profile for your app calls for that (e.g., you have exceeded an allotted bandwidth threshold).
For Ice Cream Sandwich it specifically says that the restrict background option is for data only and does not restrict for WiFi.
Related
I have developed a hybrid app running on Android, that successfully monitors the users location and displays it to the user as a moving icon on a map. This part of the app is all written in javascript and works inside a webview. The webview also communicates with my remote server to share the user's location with other users of the same app.
Up until Android Nougat this all works even when the app is backgrounded for several days. When the user returns to the app they can see where they have been with timestamps at each point. And all the other app users can see where everyone is, even if they are no actively using the app.
From Android Oreo onwards, I had to move the location tracking to a background service to get it to continue in the background and I have tested this to be still working fine even with the app in the background for a long period. Returning to the app, the user can still see where they have been. However the remote communications to the server, on Android Oreo and later, cease after approximately 2 minutes in the background.
Please could someone explain why this is, and what I could (if anything) do about it?
What is the restriction that I am running into and where is it documented on Android developer? I have set the app as not to be limited in settings/network/data saver and this makes no difference.
Would it make a difference if I replaced the webview XMLHttpRequest with similar code in background service, using volley or something similar?
All help very much appreciated.
Although I have not discovered what restriction I am running foul of, I have found that by moving the http request from the web view to the background service using volley, the server continues to be updated whether or not the application is in the foreground.
Only tested so far on Android 8,9 and 10 in the emulator. Testing soon on a real device
I am using service for sending current location of device to server continuously in background, but service is also getting killed when I swipe out the application on some phones. It's working if I remove restriction of running background services from Phone settings to the app. I will be thankful, If someone provide solution of removing this restriction programatically.
Every Phone manufacturer has it's own settings for background processing apps. As there are many android device manufacturers it will be difficult to check for every device group. I'm facing the same issue for my app. It will be better if you ask to check is there any setting regarding background services in their device.
I am running an accelerometer based android app that will run for a few months while phone is on and does nothing else. Some phone allow display not to go to sleep at all which allows my app run fine infinitely. The screen also has only a black display and nothing else apart from background accelerometer listener and occasional http posts. My question is if I remove the display screen while the app is running, would that stop the operating system and/or my app?
My question is if I remove the display screen while the app is running, would that stop the operating system and/or my app?
In short, it depends on your app architecture (otherwise i.e. music players would require to keep screen on to work). Depending on task you are really doing you may use Alarm Manager to periodically fire your code, or use Service.
I'm developing and Android application on CodenameOne that needs to send a web request every 5 minutes even when minimized. How can I achieve this behavior in order to prevent that the request get stopped or paused by the OS?
You cant do that from the activity, you'll need to create background service.
http://developer.android.com/training/run-background-service/create-service.html
Use AlarmManager to set up your every-five-minute operation. Have it trigger a BroadcastReceiver, which in turn passes control to my WakefulIntentService (or your own IntentService that handles the WakeLock, that you will need so that the device stays awake while you do your work). Have the service do the "web request".
This is still not 100% guaranteed:
The user can Force Stop you from Settings, in which case your alarms are gone and nothing will happen until the user manually runs your app again
The user might block alarms from firing on certain devices, like various SONY Xperia models, that offer "stamina mode" or the equivalent
However, it is the best that you are going to get, short of rolling your own Android OS version.
The other guys answers are correct that you need to create a service but they somehow ignored the mention of Codename One.
Under Codename One you need to create a native directory for android and just place the service source code there (just use a blank service class that doesn't really do anything). Then you need to add to the build arguments the option android.xapplication where you would state the service XML attributes.
Having said that what you are trying to do is VERY wrong and you shouldn't do it in Android! You will drain the battery life from the device in no time and the service will be killed by the OS eventually (since it will be a battery drain). The solution is to send a push notification to the device to wake up the application.
In Android 9 and newer you can prevent your App falling asleep with a battery setting.
Long click on your App -> App info -> battery -> optimize battery consumption
Here add your App from the list.Hint: maybe the menu entries have a different name, depending on your phone.
I would like to know if there is a way to lock (prevent) an application from starting.
And i also would like to know if there is a way to prevent a service(application) from starting at boot of the device
...i would like to know because i would like to create an anti-malware app.
I know this question is old, but for others stumbling over it:
Autostarts is an application that can disable apps from starting at boot time. It's the best I've found to do that (it isn't resident and doesn't kill processes like a task manager, it actually parses apk packages and reads registered actions and blocks the actions you tell it to). BUT it needs root and hasn't been updated for a while (december 2011). It works on Android 2.3 on which I tested it. Because it was discontinued, I don't know if it works on newer OS versions.
It's commercial now, but that's not the point, you need a peek at the source code.
If you search a bit, you'll be able to find the source code for an older version and see how it implements the blocking system.
I would be very interested in an application that could block certain services. NOT kill, but prevent them from starting in the first place. And the list is quite big: Facebook (OrcaService, MqttPushService, MediaUploadService, BackgroundDetectionService), Twitter, Maps (NetworkInitiatedService), Yahoo Mail Sync, etc. I don't use the features that the services provide, I even disabled some of them in the app interface where possible, but they still pop up and remain resident after exiting the application.
I would like to know if there is a way to lock (prevent) an application from starting.
Not in any supported fashion. Anything that does this is malware, and the techniques for doing it are security holes.
And i also would like to know if there is a way to prevent a service(application) from
starting at boot of the device
The user can boot their phone in safe mode (I forget the exact process, but it's something like holding down the HOME key while turning the phone on).