Can't start foreground service from background in Android P - service

To play a long audio file at a certain time (an alarm), I used AlarmManager.setAlarmClock and started a foreground service at certain time.
The app is in background at time alarm triggers, it's working properly in Android versions prior to P.
But in Android P sometimes service starts and sometimes it doesn't.
(I've included foreground service permission in manifest)

Related

what is a simple way to create an app that runs in both background and foreground in flutter?

I am trying to create a flutter app that sends notification every time there is new entry in stream builder, it works fine when the app is on or opened but I want it to work and receive notification even when it is not opened , I have used workmanager to make the task work in background but it is not working ...the code for it can be found here code
Flutter apps by nature are supposed to run on a UI thread or foreground. The background tasks you are asking for are features disclosed by each individual platforms.
In case of Android, you can create a a Background Service to accomplish the tasks you need to perform when application is closed. The link to the bacground service is as follows: Creating a Background Service
In case of iOS, see this link for creating a background task. Other platform implementation will be as like above, so please have a look into each platform specific background process.
UPDATE:
Please have a look into the package Flutter Background Service. This package helps to deal with background tasks setup from flutter side even when app is closed.
NOTE:
Q: Why the service not started automatically?
A: Some android device manufacturers have a custom android os for example MIUI from Xiaomi. You have to deal with that policy.
Q: Service killed by system and not respawn?
A: Try to disable battery optimization for your app.

How to enable altbeacon android library forground service only if entered beacon region?

I'm using altbeacon android library for beacon detection in my app. It runs foreground services as a feature by default, but I only want to run foreground services when I enter a beacon region or when one beacon is detected. This problem does not appear to be documented anywhere. Would you be able to help me?
Using AndroidBeaconLibrary 2.19+, autobind APIs can be used to easily switch to using a foreground service after a detection.
The basic steps are:
In a custom Application class, call beaconManager.startMonitoring(region) in the onCreate() method. This sets up initial beacon detections using scheduled jobs.
When you get a callback to didEnterRegion() you will need to call beaconManager.stopMonitoring(...) and beaconManager.stopRanging(...) for all monitored and ranged regions.
After step 2, configure the library for a foreground service as shown here
Again start monitoring/ranging any desired regions.
Be careful with the above approach, as standard Android will block starting a foreground service from the background in Android 12 in some cases. In addition, some non-standard OEMs already do this on earlier Android versions.
In general, the recommended practice is to set up the foreground service only if (a) the app is in the foreground or (b) you know the user has recently interacted with your app's UI. If Android blocks your app from starting the foreground service, starting monitoring/ranging from the background with a foreground service configured will cause your app to crash. Because the conditions that may cause this are complex and hard to predict, this technique may lead to unexpected crashes and related bugs.
One alternative to the above is to use the new IntentScanStrategy introduced in the library, which allows faster background scanning without the need for a foreground service.

Flutter : keep socket io open while app in background (IOS)

I'm building app that uses flutter_pllayout and adhara_socket_io
App Scenario
When app is launched, it connect to a socket and keep receiving audio urls at random times, then it plays this url, it works fine on android
Problem
but in iOS when user switch to home or any other app (when my app is in background) it seems to close the connection of the socket and continue when app become in foreground, is there any way to keep the socket connection open ?
Background tasks on iOS are very limited. Only works if you play audio constantly with the correct Info.plist details.
If you want recurrent (cron job task) client side, use: BGTaskScheduler (for iOS), one other option is using a cron job triggered push notification task (you can even user Firebase Functions for that) server side.
Official docs: BFTaskScheduler, Background Behavior iOS, Enabling Background Audio
and no, sockets connections can't keep opened all the time (not even on Android on latest versions of their OS). Again, use cron job tasks to achieve a similar behavior.
Officials docs: Android Background limitations

Keep infomed for Device location will Application in Backgroud

I'am developing an application that keep a WebService informed for device location each 5minute (for exemple) :
So when the application leave the foreground execution and enter background I have to lunch a timer who
1 - Update the device location
2 - Send the location to a web service
How can I perform this action ? Or do you know any code exemple that I can follow to achieve this design ?
Thank you for your help !
Doesn't look like anyone gave you a solid answer, so allow me.
There is no iOS approved way of running a 5 minute (or any minute) timer in the background (unless your app is VOIP or music). What you CAN do is register your app as requiring location services in the background (edit the info.plist and add a key Required Background Modes and then add a value App registers for location updates. What this means is your locationManager:didUpdateToLocation:fromLocation: method and corresponding region monitoring/SLC methods will fire on location change, but Timers will not work at all.
The reason why is that timers require a run loop (a thread executing code) to be running and must piggy-back on that thread, but when the app is in the background even when executing code from the LocationManager, the run loop that executes the code almost always finishes before your timer would go off.
Hope this helps!
you cannot do this (on iOS).
(unless your app requests permissions that are meant for Navigation apps)
Hello #Aladdin Gallas,
I have developed a simple application that supports background location updates for iOS and Android in Xamarin.
The app pushes a new location every 2 seconds (you can change it to 5 minutes if you want to).
There is a feature I haven't been able to add; it is to keep the service up when the user closes the app (for iPhone). Other than that, as long as the app is open, even if the phone is locked, the service keeps running.
You can take a look at the application as a reference if it helps.
GitHub Repo

Running app continuously in background on 3GS and 4

In order to run the app continuously in the background on the 3GS and iPhone4 on OS4.1
is it simply enough to call BeginBackgroundTask in the DidEnterBackground callback and then
NOT call EndBackgroundTask ie to leave it running. I understand this will run the battery
down but that is ok as my users will be running on power.
If this is not the way to do it , can someone say how to keep the app running (not suspended)
Thanks
You cannot keep the app running on the background.
You can declare some tasks that the system will run in background.
According to the Apple documentation:
Support for some types of
background execution must be declared
in advance by the application that
uses them. An application does this by
including the UIBackgroundModes key in
its Info.plist file. This key
identifies which background tasks your
application supports. Its value is an
array that contains one or more
strings with the following values:
audio - The application plays audible
content to the user while in the
background. location - The application
keeps users informed of their
location, even while running in the
background. voip - The application
provides the ability for the user to
make phone calls using an Internet
connection. Each of the preceding
values lets the system know that your
application should be woken up at
appropriate times to respond to
relevant events. For example, an
application that begins playing music
and then moves to the background still
needs execution time to fill the audio
output buffers. Including the audio
key tells the system frameworks that
they should continue playing and make
the necessary callbacks to the
application at appropriate intervals.
If the application did not include
this key, any audio being played by
the application would stop when the
application moved to the background.
In addition to the preceding keys, iOS
provides two other ways to do work in
the background:
Applications can ask the system for
extra time to complete a given task.
Applications can schedule local
notifications to be delivered at a
predetermined time. For more
information about how to initiate
background tasks from your code, see
“Initiating Background Tasks.”