Run some code when app is killed - android

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

Related

Android background Service vs foreground Service

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

keep service running in background in android

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.

How to make an application run continuously in android?

I am creating an application which sends the location information to the server. But when the back or home button is clicked the application is stopped. Is there a way to make the application run continuously in background and sends data to the server ?
Use a background Service. Services are application components which can run in the background without user interaction. A good tutorial on Service in android is from this.
You probably want to start the service on launch of your Activity, or you can register for system broadcasts like BOOT_COMPLETE to keep your service running from boot up.
However, keeping your service running throughout without user knowledge is a bad idea, and drains battery also. You probably want to wake up the service using AlarmManager, do processing, schedule next restart, and exit.
You need to run a Service class for your own application.
Refer docs for more information.

Run an android application continously for testing purposes

I am trying to run an android application continuously for testing purposes and noticed that the app stops after few hours. So I implemented an AlarmManager to start the activity every few hours, even with this I cannot run the app with out stopping.
I see the AlarmManger restarts the activity for few times and even it dies along with the App. Could someone suggest me how to achieve the functionality.
I really appreciate your help!
Thanks.
PS. I am testing the phone functionality using android like: cameraTest, ModemTest(playing audio, video files), Making Call, Bluetooth etc.
You could create a Service that restarts the app every x minutes or something. From the docs:
"A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. "
More here.
You can create a Service with a thread continously monitoring your application status(whether running or not) and it can launch a broadcast message to your application in case it is not running.

Android Services

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

Categories

Resources