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

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.

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 use network connectivity plugin in background process in flutter

Is it possible to use connectivity plugin in background process?
I don't know how to call dart code in background there is little about this in docs.
I need this functionality to sync offline data with backend whenever there is a connection wether app is in foreground or background and app is in killed state like the way Whatsapp syncs messages.
You can use existing plugins for background execution (on android & ios usually in a roughly 15 minute interval)
https://pub.dev/packages/background_fetch
There is also a article dedicated to using isolates for background executions
https://flutter.dev/docs/development/packages-and-plugins/background-processes
https://medium.com/flutter/executing-dart-in-the-background-with-flutter-plugins-and-geofencing-2b3e40a1a124

Using both standard and significant-Change Location Service

I like to extend my app which is using Standard Location Service with the capability to notice the app when an Location change happend even when the app is not running or in Background.
Apple proposes for this the use of the significant-Change Location Service, which would be ok to save battery power and the accuracy would be fine for me.
But is it possible to use both the standard and significant-Change Location Service together in one Project ?
Found a solution to implement this with the help of the Apple Developer Forums. I did the following:
Specify location background mode
Use an NSTimer in the background by using
UIApplication:beginBackgroundTaskWithExpirationHandler:
In case n is smaller than UIApplication:backgroundTimeRemaining ,it does works just fine, in case n is larger, the location manager should be enabled (and disabled) again before there is no time remaining to avoid the background task being killed.
This does work since location is one of the three allowed types of background execution.
Note: Dont loose time by testing this in the simulator where it doesn't work, works fine on my phone.

iOS background application network access

I have an application that requires location tracking and I think it fits squarely within one of the allowable background models. However, I need to post to a network service when there are significant changes of location. I've seen write-ups that state network access is prohibited in background processing, but I didn't read that in Apple's docs.
Does anyone know if it's kosher (wrt Apple policies) to make occasional and very quick network updates in a background process?
Good point, according to the Apple documentation only the following usages are allowed in background and each service should be registered:
audio—The app plays audible content to the user while in the background. (This content includes streaming audio or video content
using AirPlay.)
location—The app keeps users informed of their location, even while it is running in the background.
voip—The app provides the ability for the user to make phone calls using an Internet connection.
newsstand-content—The app is a Newsstand app that downloads and processes magazine or newspaper content in the background.
external-accessory—The app works with a hardware accessory that needs to deliver updates on a regular schedule through the External
Accessory framework.
bluetooth-central—The app works with a Bluetooth accessory that needs to deliver updates on a regular schedule through the
CoreBluetooth framework.
Other than this services, you can create a Finite-Length Task in the Background that actually give you the possibility to end a network process.
This can be important if your app is performing some important task,
such as writing user data to disk or downloading an important file
from a network server.
Regarding your question, it's not really clear if you can or not can do a quick network connection if you've a location service running in background. I would say YES for a short connection, but not totally sure. Since iOS 4.0 this usage was denied and clear in documentation, now that part has been removed.
Yes if you use background for just quick connection. Apple won't allow you to run in the background as you want.
NO If your app does not fall in the Voip, music or GPS category; then you can't run in background.
more here: Update my app when it is in background
You could use ASIHTTPRequest.
ASIHTTPRequest has a property setShouldContinueWhenAppEntersBackground:. default is NO, you may turn on YES so you have background network process.
Apple documentation seems a bit unclear on the strict policy. There are definitely applications in the app store that make sporadic network calls while running in the background as a location-based application. An example is the Geoloqi application.

Which applications launch into background state?

I'm reading through the multitasking documentation, and it has a few references to apps which launch directly into the background state, never entering the foreground state. Is this really allowed for regular apps? Can anyone give me an example of an app like this?
VoIP apps are the biggest one to use this feature. Basically a VoIP app can register itself with the system to be notified when network traffic is intended for it at which point the app takes over handling the incoming traffic (i.e. receiving a call). Skype and Viber both use it.
From the iOS Developer Library (emphasis mine):
Including the voip value in the
UIBackgroundModes key lets the system
know that it should allow the
application to run in the background
as needed to manage its network
sockets. An application with this key
is also relaunched in the background
immediately after system boot to
ensure that the VoIP services are
always available.
The significant location changes backgrounding service also allows an app to be updated with the new location even if it's not running.
Other than those two cases, an app can't do anything from a terminated state until the user launches it.
When you use location manager with significantchange notification, App gets backgrounded automatically if the app is killed, when there is a location event