I have built an android app that performs some downloads followed by some time consuming processing. My goal here is to do the downloading and the processing in the background. I have read about background and foreground services, but I am not able to understand them properly and which to use where.
I have built the rest of the app with ionic. Now I have to make the app work in background. I have tried cordova-plugin-background-mode available in ionic but unfortunately its not maintained anymore.
So what should I do to my app in android studio to make it support background processing.
Also is it possible to combine android packages to an ionic project after building it?
Thanks in advance.
First understand about Android services: Three different types of services:
1. Foreground service: is a service that stays alive even when the app
is terminated. Foreground services continue running even when the
user isn't interacting with the app.
List of Apps:
Music player app that plays music in a foreground service
Fitness app that records a user's run in a foreground service
Navigation app, allows users to get turn-by-turn directions
Even you perform your download
Note: To download and the process in the background Google recommend you to use WorkManager.
Let's understand background work:
An app is running in the background when both the following
conditions are satisfied:
None of the app's activities are currently visible to the user.
The app isn't running any foreground services that started while an
activity from the app was visible to the user.
2. Background service: is a service that runs only when the app is running so it’ll get terminated when the app is terminated. It performs an operation that isn't directly noticed by the user.
List of Apps:
Downlead data from server
Continuously share location
Sync data with server also use workmanager
IOT Apps
3. Bound service: is a service that runs only if the component it is bound to is still active. A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.
All above apps can be bounded or not
Related
Not sure if anyone would be able to help out, I have an application that scans and interacts with Bluetooth devices, the application works fine however I'd like to utilise Headless JS background services so that the scanning and interactions work at all times.
The process which I'd like to replicate as a service does the following
scans for Bluetooth devices
when a device is found commands are sent and data is received
the app processed the data and posts it up to the API
the app continues to scan and wait for the next device
I'd like to know how I could manage such a service so that it complies with the following
I would like to ensure the service is always running and there is only 1 instance of it running at any time.
The service will need to be able to start straight away or start as soon as the app is out of memory(i.e. keep a foreground instance of the code running and switch to the background service once that's unreachable).
Any advice on how to set up the scheduling of services would definitely help out!
I am developing a chat application in android . and need to keep service running
even after exit from application .
I am usin
return START_STICKY;
in onStartCommand() of my service .
but because of limitation of services in android oreo , service will destroyed after seconds when exit from application.
So far users lost new messages notifications.
I can not use Fcm beacause of local networking and no access to internet.
And I can not use ForegroundService . (because Of Employer's request to not showing any notification) .
When I checked running service in android mobile setting , there are some
apps that their service not killing like Es file explorer , Zapya , ...
How they keep their service running without foreground service .
And What should i do .
Show in blow image , some apps services are running without any notification .
Based on the documentation:
The system distinguishes between foreground and background apps. An
app is considered to be in the foreground if any of the following is
true:
It has a visible activity, whether the activity is started or paused.
It has a foreground service.
Another foreground app is connected to the app, either by binding to one of its services or by making use of one of its content
providers.
Reason Es FileExplorer can do could be (its just my opinion) following:
Es FileExplorer (is quite cheeky when it comes to taking advantage of some loop holes) have several content providers but one provider, FileProveders which is some how manages to have com.android.providers.settings connected to it. I guess this connection makes it foreground. They virtually have all the possible intent-filter registered for almost all the scheme. Anything you try to share or access, could trigger them some or the other way which keeps its process in use (you can just click on the details and you will find LocalCService of app running).
But for your app:
If you can't use FCM, ForegroundService and can't have visibility to user, then only option is to perform task periodically. You can use WorkManager. The only limitation is minimum duration for scheduling is 15 minutes. Refer to my answer for scheduling work with WorkManager and WorkManager vs Service for usage of WorkManager.
According to the Android developer website https://developer.android.com/guide/components/fundamentals.html
A service is a general-purpose entry point for keeping an app running in the background for all kinds of reasons.
And on several occasions, i read that a service is (also) used as a means to tell the system that the app requires doing some work in the background.
What is the difference in my Application object creating a sticky service and starting it and it creating a POJO that does the same work?
When the app enters the background (home button) how does the existence of the service change how the system treats my app? Will the service (which runs on the main thread) cause the system to schedule my main thread higher or not reduce it a priority while in the background ? Will it do so if there is no service but a POJO doing some work?
Neither will receive any notification of my app entering the background or coming back to front, neither will be connected to any activity (but could provide functionality for activities to connect to them).
So how exactly does the use of a service change how the system treats my app when it is in the background?
Somewhere it was mentioned that if there is a service running the app will be restarted should it be killed for any reason, however, the service will be killed along with its process (we are talking about a service running in the same process as the rest of the app) but this does not have anything to do with "running in the background" as the android guide mentions.
In addition, the Application object could bind to a service, holding it like a POJO. What would be the difference here, regarding how the system treats my app in the background?
I'm trying to develop a mobile application which interact with ibeacons !
I'm developing this application for iPhone and android ! But i have problem !When i was developing iphone app, everything works,
i receive my notification even if my app is killed !
But on android i don't know how can i develop that !
If my application is on background, it works but if i kill my app, nothing happen !
Do you have an idea to run my code even if app is killed?
Thank by advance!
If you want run some code in background even if your application is killed you should use services. From google documentation:
A Service is an application component that can perform long-running operations in the background, and it does not provide a user interface. Another application component can start a service, and it continues to run in the background even if the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). For example, a service can handle network transactions, play music, perform file I/O, or interact with a content provider, all from the background.
If your app runs in scheduled times you can use JobScheduler or AlarmManger. JobScheduler and AlarmManager wrok with services.
You can use Service. It will still working even if your activity is killed.
https://developer.android.com/guide/components/services.html
What are the disadvantages of running services in Android in Foreground.??
I recently read that if you want your services to last longer and not get killed easily we need to run the service in foreground.
What you read is right. It depends on what you want to do with your application. If your service does something which should not be interrupted without explicit user-interaction you should start it as a foreground-service. This assures that the service won't get killed if more memory is needed by other applications. Also you have an ongoing notification displayed so that the user is aware of what is happening and you can provide functionality to your notification such as opening an activity when tapping on the notification etc. Examples for this may be a music player service or a download service. If you have a service which does not have to run necessarily after leaving the app you should choose a service started in background, so that the memory can be released if it is needed for other tasks. Some more information you can find here: http://developer.android.com/reference/android/app/Service.html