How to receive a callback even if you kill the app? - android

I'm developing an Android app.
I want to continue to receive callback even if I close the app. (For example, Bluetooth connection or ConnectivityManager.NetworkCallback)
I know that such a way is a foreground service, but I don't want to use it.
Because if you use the foreground service, you have to make notification essential, but I don't want it.
Is there a good way to get callback even if you shut down the app without using the foreground service?

Related

Foreground Service or standard service?

I have Fragment which is basically a View that shows any incoming messages for a user. I want the create something that will check every 5 seconds for any new messages, and if found will append them to my ListView which holds the messages. My question is, from what I have read, a Service is the way to go with this. However, since I will be communicating with the app from this service I'd like to know which service I should use.
Should I be using a Foreground service, or just a standard Service?
My goal is that where ever the user is in my app, I will be able to receive some notification that a new message has come through and then perform a function when that happens.
I want to code this properly and according to best practices.
If you just want to call a method on your service when user is in your app, you just need to use a sticky service, but if you want to call this method even when user swipe your app away from recent apps you should use not_sticky service.
Foreground service are most used for cases that u don't want your task stop even for a second after swiping your app from recent apps e.g. playing music in background.
But in your case the best choice is to use postDelayed() and set 5 seconds delay for it and get rid of service.

do I have to use a service for a socket in a thread going in background mode

I have an app that is doing many things and also is running a thread to listen to a port using a socket. I want to be able to switch for another app (for ex email) but still my app would continue to listen to the port.
I suppose that I have to use a service?
Also I want to listen to the next incoming request. So I believe that I have to use the bind mechanism to re-run through the socket.accept() code?
A service will continue to run even if your app is closed. So yes, a service would be the way to go. You can also bind to the service when your app opens so that you can communicate between your service and your app.
Also I want to listen to the next incoming request. So I believe that
I have to use the bind mechanism to re-run through the socket.accept()
code?
You dont necesarily need bind for this. Your service can listen for incoming requests by itself.
Do note however that a service does not run on a seperate thread so if you want to do intensive work, consider spawning a different thread inside the service. You can also use an IntentService which will handle the threading. But you can only run one IntentService so if you need multiple services, a normal Service would be a better choice.
also read:
https://developer.android.com/guide/components/services.html
and: https://developer.android.com/reference/android/app/Service.html
EDIT:
The service can stop itself when the app is closed. If you simply press the home button to switch to another app, the app will only be paused and the service will continue to run. You can however influence the life of the service.
Check: Android Service Stops When App Is Closed

Multiple background services with one ongoing notification

I am writing an app that connects to a Bluetooth device, continuously receives data from it, and stores in local db. Certain data received requires system alert to pop up. There is a main activity which just displays the status of connection and data received. It all works just fine so far, including the popups.
Since the app requires to be run in background I have implemented a "bluetooth connection" service that manages the BT connectivity, and displays ongoing notification in order to avoid being killed. For coding clarity reasons I would like separate background service to collect all data, and log it (instead of having BT service do all the work). I also prefer loose coupling between my app components, so am using GreenRobot's event bus for all IPC. As a result my BT connection service is completely unaware of any data collection/logging code - it just dispatches a message to event bus and I'd like to keep it that way.
Now I'd like to have my data collection/logging code to be run as another background service. Is there a way to ensure it runs as long as BT connection service is running? And without displaying yet another ongoing notification or tightly coupling the code between two services?
You could let your class extend service so in this case you dont have to make a notification for it. Basically it keep running in the background without the need of displaying notification on the status bar. Make sure before you exit your app to stopservice() otherwise it will keep running until the device restarted or in somehow the user force stop your app from application manager inside of the settings.

Android KitKat: Implement a background service to do something when an application is unloaded (or loses foreground)

I want to implement a service (or similar) on Android KitKat (4.4.2) in order to detect which is the foreground app and make something depending on which app it is "foregrounded".
I have read a lot of threads about determining which is the app is the foregound ON THAT MOMENT (https://stackoverflow.com/a/14044662/1683141). But I'm not able to see any thread about keeping this service continuosly monitoring in order to detect any changes on foreground. Kind of loop? Event registering?
For example, I want to be notified when LINE (messaging app) has or loses foreground. So I suppose the service has to be registered to some kind of event (I think Broadcast here is useless) in order to be notified and then take some action.
I don't know if that is possible. I hope it is.
Thank you for your help.
You are unable to keep your service alive if system decide to kill it. You are also unable to keep your service alive if your app is "unloaded" (whatever you mean), because your service is part of your app (and APK) and will be unloaded too.

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.

Categories

Resources