Keep notification service in background like stackoverflow app - android

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.

Related

Android (API 28) - Receiving implicit intents in background

I am wildly confused with the current Android background service possibilities and need some help from an expert :)
The "xDrip" application is broadcasting its information via an (I guess implicit) intent with the action name "com.eveningoutpost.dexdrip.BgEstimate". I want to receive these intents. As it is not allowed to put them into the manifest I registered them dynamically in the onCreate() method of my application class. This is working great as long as the app is running (even when the app is not open on screen). But if I swipe it away in the app overview, it does not receive the intents any more even though I do not unregister the intent.
My question is now: What would be the most suitable way to receive this (implicit) intent reliably all the time? So also when the user swipes the app away in the app overview screen?
Cheers!
This is working great as long as the app is running (even when the app is not open on screen).
More precisely, this is working great so long as your process is running.
But if I swipe it away in the app overview, it does not receive the intents any more even though I do not unregister the intent.
More precisely, you stop receiving the broadcasts once your process stops running. Note that this will happen naturally while you are in the background, even without a manual swipe of your app off of the overview screen.
What would be the most suitable way to receive this (implicit) intent reliably all the time?
Technically, it's impossible.
You will get close if you use a foreground service, to keep your process around longer. However:
The user can still swipe the app off the overview screen, and this might still stop your service and terminate your process
Even a process with a foreground service might be terminated by Android automatically after a substantial period of time
Your app will be consuming system RAM the entire time, which users may or may not appreciate

Android and background processes

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.

How to send local notification android when app is closed?

I'd like to know if it's possible to send a local notification to the device when the app have been opened then closed.
It works already when my app is opened and when it's in the background.
Thanks
Edit : I think i wasn't clear enough:
I want to send a local notification at a given time even if the app is not running at that time.
By "Local Notification" i mean a Notification (android class) created in my app and send by using an AlarmManager and a BroadcastReceiver.
closed -> killed the process of my app
Most means of "killed the process of [your] app" will leave your alarms intact, and so whatever you have scheduled will remain scheduled and keep being invoked as you set up.
I want to send a local notification at a given time even if the app is not running at that time.
Again, AlarmManager is not dependent upon your process being around, so long as it can invoke the PendingIntent that you supply. So long as the BroadcastReceiver you are using is registered in the manifest (and not via registerReceiver()), it should work fine.
If the user force-stops your app -- usually via the Settings app -- then not only will your alarms not be invoked, but your code will never run again, until something explicitly starts up your app (usually the user tapping on your icon in the launcher). There is nothing that you can do about this.
it's hard to explain things when you do understand fully and when english is not your native language
There are many Android developer support sites, offering a variety of languages.
you need to create a service, http://developer.android.com/guide/components/services.html

Swipe away my application from the recents activity kills my service

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.

Notifications on an app after it is shut down with the 4.0 android task manager?

I'm using C2DM in my application, and I have a receiver, which sends data to a class in the application. The class creates a notification and notifies the notification manager to post it.
The problem is that this does not work when the app is forced close manually through the settings, as this also (apparently) shuts off the broadcast receiver.
What I get though is that when an app is shut off with android 4.0's new task manager (the one thats similar to 3.0 but a user can also swipe an app to the left or right to shut it off) it behaves differently: the broadcast receiver is still working, as I get the intent from the C2DM message, but for some reason my phone still plays the notification noise, whilst no notification appears in the tray.
I can't figure out what's happening, because there is no way for the sound to play without the notification to appear, as the sound is attached to the notification and plays when it's posted, no other way. But no notification appears.
Any insight on why this might be happening would be awesome, or what the new 4.0 task manager actually does to apps when you swipe them off the list.
Thanks.
Figured it out, the broadcast receiver was still responding but just failing because it was retrieving things from a class that was part of the main app and was now dead, so now the things it needs are stored in sharedprefs, and retrieved before the notification gets sent.
So to answer the question, no swiping an app from the task manager in 4.0 does not "force kill" the app in the same was as the force kill button in the applications menu in settings. It does kill off the app in such that next time you open it, all the activities restart from scratch, just like if you had been in the last remaining activity and pressed back, hereby calling finish() on the last alive task and shutting down the app. broadcast revivers (and services i assume) still are running afterwards.

Categories

Resources