I created a service to handle my AsyncTasks like uploading a file on a server or downloading one. When I swipe away my app from the recent activity menu, my service is killed. Is it normal behaviour ? If so, one solution would be to set it as a foreground service with startForeground(int, Notification) but I must display a notification and I don't want it as I'm already displaying one for each AsyncTask running.
How does the "play store" app download applications and keep the downloads alive even if I swipe away the "play store" from the recent activity menu ?
Is it normal behaviour ?
Yes. Android, at the user's request, terminated your background process.
If so, one solution would be to set it as a foreground service with startForeground(int, Notification) but I must display a notification and I don't want it as I'm already displaying one for each AsyncTask running.
Please do not show a separate Notification "for each AsyncTask running". At most, show one Notification. Few, if any, apps are important enough to warrant separate Notifications.
Related
I have a food ordering app and I need to inform the restaurants of a new order. I have a Capacitor app which often runs in the background of tablets/phones of the restaurant. As a result, they sometimes miss an order.
In order to solve this, it would be great if I could ring the device as if an alarm goes off or if the device gets a call. Then they can swipe away the notification to stop it or something like that, to make sure they saw it. I would choose the sound myself so that it isn't obnoxious.
Is anything like that possible?
yes it's possible but,
you should know the following:
for Android:
1-create a foreground service to keep notification appears to the
restaurants, in this case the restaurant can not hide the
notification.
once the restaurant receive the notification just
sent an event to the foreground service to handle the action and the
data.
start the order activity from foreground service, once the
activity is created just play a sound.
for IOS,
it should be the same but i don't know how to create a foreground service in IOS.
I want my app to silently start in the backgorund, without showing any activity on the screen.
It has a service which needs to perform 2 upload tasks.
I'm learning about service, but all boot-up launch of apps talks about showing the activity.
I need no activity to be shown.
Is that permitted after Oreo?
You didn't say what should trigger your app from background. I presume some kind of an Intent. As far as I know there is no way to start service in the background since android O without showing anything to the user. You should start your service using startForegroundService(), then show notification, perform your 2 uploads and turn off the app (hidding notification too). If those uploads aren't huge, it will be pretty quick, and in most cases user won't even see the notification.
I have a service, which monitors a server for changes.
When I open the app and just press my home button to leave it, all works fine.
But when I kill the app from the "recent apps" view, I don't get any notifications anymore.
I know that there is way, to use startForeground. But then I have to display a notification.
But how does the stackoverflow app work? There is no notification, no entry in the android account manager or anywhere else. But without starting it and without keeping it in the recent view, I get notifications for new answers.
How does it work?
Try to start service in another process. This way even if you kill your app through "recent apps", the other process (in which the service runs) will continue running. Also, on the onStartCommand of service, return START_STICKY. This will tell the OS, if for any reason you need to close the service run it again when you have enough resources.
Sorry I do not have enough reputation to put it as comment.
I wrote an bluetooth app with my bt-connection established in a service, so the connection is still alive when I minimize my app.
But when watching my task manager, my app is still there.
And when calling onDestroy in my app, I have to stop my Service.
But other apps like telegram or skype (whatsapp too I think) aren't in my task-manager visible but by having an incoming message they notify me nevertheless.
How is this even possible? How can I write my bluetooth connection like this, that I can really close my app and anyway the incoming messages will be handled?
The other apps might be having some light weight service running in other process which gets the data for the main app.Go to settings->application manager->running processes..you will see all the service..
Other mechanism which apps use is port-directed sms. In such a scenario you don't need any service running.However port directed sms doesnt work on all the phone and for all the apps.
When you put remove your application from the foreground, Android keeps the activity on the stack again in case you will go back to it (unless you explicitly destroy the activity). So this maybe one reason why you still see it in the Task Manager.
You cannot kill entirely the app and it will still post messages. Your service will be running in the background and it will be visible in the app Manager->Running Services.
However if you destroy your activities the app it will not be visible in the app list of the Task Manager.
I have an alarm application where users can set multiple alarms. When an alarm goes off the app simply displays a Notification which when clicked will launch the main activity screen and remove the Notification.
Each alarm gets its own Notification which is where my question comes in; if there are multiple Noitifications showing, when the user clicks one I would like to clear all of my app's Notifications. To do this I need to track the IDs of the Notifications currently showing. What is the recommended way of doing this?
I suppose these will need to be persisted as my app's process could be killed as soon as a Notification has been created and displayed. Is my thinking correct?
To do this I need to track the IDs of the Notifications currently showing.
Or just call cancelAll() on NotificationManager, to cancel all your outstanding notifications.
What is the recommended way of doing this?
To be safe, you should use some persistent data store (e.g., file), as your process might go poof between alarms.
I suppose these will need to be persisted as my app's process could be killed as soon as a Notification has been created and displayed. Is my thinking correct?
It could certainly be killed before the next alarm, as discussed in your other recent question.