Sticky Service and Intent Service gets killed when the app is destroyed - android

In My app I am trying to download images even though user has force stopped it and exit it using recent app manager by swiping . Why I want this behavior, it has only one reason and that is :
Because I want User to stop the download from my button given in activity
So For this I have made async task at first place , it was working nicely even the activity is destroyed, But When I destroy/kill my app using recent app manger the app gets killed and so the service
Then I moved my async task to the Service which is Sticky , I thought it should not get destroy when the user closes the app from recent app lists, but The service stops. though some times it start again , but due to some reason I just want it to not to stop and download like a google play store does. What ever you do , you can not stop downloading of app , until and unless you use stop button in the google play store app.
Then I read about IntentService, about intent service I read that ,
You can run intent service and forget about it , as it stops itself
after completion of task.
So I though this is something , which could not be killed even user stops it or close app from recent app list by swiping , as intent service would perform its task and would stop itself
But All in vein. I have read many things about the service and how to cure service from being not killed. I came to know about starting service as forground.
But what other ways do other Top class apps adopt to keep the background working all the time , like security apps , gps based apps, and antivirus apps whose services run all the time on background. You can examine same behaviour inn google play store app , you can start downloading of any app and exit the play store app , even from recent app tray , but it would continue to download until and unless you go to notification bar click notification , get navigated to place where you finally see stop downloading button to stop downloading the app.
So How can I achieve this ? what is wrong with service or what else can I use to get the same behavior as google play store has.
Please guide me . Your comments and discussion would be appreciated.

It sounds like you want to run your service in a separate process from your main application so it does not get killed if the app is removed from the "Recent Tasks" list.
This is made possible by using the "Process" attribute explained in the documentation here:
http://developer.android.com/guide/topics/manifest/service-element.html#proc

What you need is a ForegroundService. The android developer guide about services states that:
A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

You can use return return START_STICKY in onStartCommand in service

Related

How to make sure a service that runs when the Application is closed does not get killed?

I am developing a Fitness Application as part of my Bachelor Thesis, and want to keep track of step counts even when the application is completely closed. For this I am currently starting a service that utilises the built in Sensors "Step Counter" and "Step Detector". After some testing I found out that sometimes my Service gets killed and no longer keeps track of the steps taken. I left the phone on my desk overnight and walked around in the morning then I opened the application and the steps I took in the morning were not tracked, whereas when I close the application and immediatly start walking the tracking of steps still works.
Is there a way to make sure that my Service does not get killed?
Would the use of a Foreground Service solve my issue and are there any alternatives to using a foreground service?
Foreground Service is the only way if you want to assure that the service will not be killed.
The reason for this is that the foreground service always shows a notification to the user and can be killed by the user if he wants to, this is especially important if you want to know for sure what runs on your device.
All previous methods of making permanent running services are deprecated starting from android 9, when a new privacy policy was introduced.
Basically you need to keep service running in the background,
Here is the workaround to achieve this
https://stackoverflow.com/a/58162451/7579041
above link is useful for Stock ROM & Custom ROM Devices like OnePlus, OPPO, VIVO, etc
I hope this will help you out

How to close application after a while user does not interract with it in android?

I have android app using google maps API. When the app is running and user doesn't interract with it app still works and system doesn't go to sleep. However I need to close it if user doesn't interract with app some while. Is there any solutions?
There are a lot of posts regarding this topic already. A key point is that the OS handles this for you and it isn't necessary to force this behavior. If you really want to force it you could have a scheduler in your application triggered by the onStop() method. When the scheduled task executes you can then call one of the functions mentioned in the links provided below to kill the app.
How to quit android application programmatically
A timer that will kill android app after idle for certain time?

Android Service killed low memory - how to let user know

I have an app who's activity starts a service to play a downloaded podcast. As the user pressed the BACK key to get to the hope screen the service continues to play the podcast as expected. However if the user launches a broswer (doesn't matter which one) and navigates to a page that loads a bunch of high res images, (of which I can recreate over and over), my service is being killed by the OS due to low memory(I'm guessing).
As a result I'm getting nailed for having an app that "crashes" when it is not crashing, the service is getting yanked.
The STICKY flags don't give me the option of exiting gracefully or letting the user know what happened.
How does a service get around letting the user know that it was terminated because of an outside app?
Make sure your service is running in the foreground. This has the bonus of showing a notification whilst your service is running too.

Started Service stops if application is forcestop from download App

i made a sample android app,which starts a started service by calling startService(serviceintent).
it works fine,but if i forceQuit my application from settings>app>downloaded>force_Quit.my service stops and even destroyed is not called.
i studied for 3-4 days and know about start_sticky in StartOnCommand method.i am able to achieve all aspects of service.
I want to know whatever i am achieving that service stops and doesnot restart automatically even if started as Start_Sticky is normal behaviour according to android.Can i make it restared if user force quit my application.
my manifest is correct i uses process tag also.
if i forceQuit my application from settings>app>downloaded>force_Quit.my service stops and even destroyed is not called.
Correct.
Can i make it restared if user force quit my application.
No. Nothing of your app will run again until something uses an explicit Intent to start one of your components. Usually, that means that the user taps on your icon in the home screen launcher, though there are other explicit-Intent scenarios (e.g., GCM message).

Android service, need a service that keeps running

I am a beginner in Android development. I want to build a service that once started, must keep running.
Step 1.) Once user will install the app give some input, service will start. (i'll use IntentService)
Step 2.) Now user moves to other app/close my app. I want to make sure Service should keep running and processing/saving info with current timestamps.
Questions:
1.) How I make sure that the service keeps running/saving data even if user returns after say, 1 month on my app.
2.) I read that every app is a new user and separate VM is provided by the Android, question is when an app is considered to be closed?
3.) If my app closes, related services will also close then?
1.) How I make sure that the service keeps running/saving data even if user returns after say, 1 month on my app.
Once user installs app, you can start STICKY service but make sure you inform the user as well as Android OS by making it run on foreground with on going notification. it will help your service to run for long time without being killed by Android OS.
1 month is long period, user may reboot the cell in between. and you will need to restart your service. so you should register for reboot complete event .
2.) I read that every app is a new user and separate VM is provided by the Android, question is when an app is considered to be closed?
There is no condition like considered to be closed.Actually, when user installs app, it is by default in stopped mode, it gets activated when user opens your app for first time. and application can go back to stopped mode again whenever user force stop your app.
3.) If my app closes, related services will also close then?
If you are considering that app is considered as closed when user don't use the app,your service won't close unless Android OS kills it in low memory situation.you may make it foreground as suggested earlier to convey Android OS that you are doing important things,nothing is wrong,so kill me only at last when you don't have any other options left

Categories

Resources