keeping background service alive after user exit app [duplicate]
Asked Answered
D

4

14

I'm trying to create a service that will do background jobs for me even after the user closes the app from the running processes menu(by shifting process out of the screen).

What I tried to do is create service in a different process by declaring it like this:

  <service
        android:name=".service.Service"
        android:enabled="true"
        android:icon="@drawable/ic_launcher"
        android:process=":my_process" >
  </service>

and the onStartCommand() is:

    @Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}
Deli answered 20/7, 2013 at 23:45 Comment(4)
Hi, have you come into a solution? I've been searching for hours to solve this issue, and I wasn't able to come to a solution. Your code looks exactly like mine.Smiga
I have solved this by simply starting my service using this line: ContextWrapper cont = new ContextWrapper(getBaseContext()); cont.startService(service);Smiga
related #16651509Showroom
if you show a notification from that service, it will prevent os from killing itDionysiac
D
9

According to the Android documentation you can achieve this behavior by using the attribute:

 android:isolatedProcess="true"

By the way, I know that it's not answering the question but it might help some people as well - lately, I found out about a great third-party lib that Evernote developers have created. Its name is Android-Job and its aim is to create jobs that run on different processes and can become active again even after a device reboot, it's amazing.

Deli answered 19/5, 2016 at 8:8 Comment(5)
This library is very old. Will it be fine to use it?Maledict
Yes we are using it extensively. Not sure but i think it gets new commits every now and thanDeli
Looks like the service is not even getting started after adding isolatedProcess=true on Oreo :(Eden
is ti still work for latest android versions ?Bradytelic
@Bradytelic Sorry for the late response. I have no idea. I abandoned this project years agoDeli
A
1

Actually there are different kinds of services you can implement. Use a Service instead of IntentService. There you need to look at START_STICKY , START_NOT_STICKY and START_REDELIVER_INTENT you can keep your service running in background even if your activity dies. Android services also look at AIDL services

Alfi answered 14/2, 2014 at 7:22 Comment(2)
I used START_REDELIVER_INTENT, but my service still stops when user closes the app.Jephthah
I used START_STICKY,but my service still stops even the app is force closed by user.Japanese
S
0

For this you can use Handler and Runnable class

   @Override
    public void onCreate()
    {

        Toast.makeText(context, "SMS SERVICE START", Toast.LENGTH_SHORT).show();


        handler=new Handler();
        runnable=new Runnable() {
            @Override
            public void run()
            {

                    TASK();
                    handler.postDelayed(runnable,180000);
// 3min delay
            }
        };

        handler.postDelayed(runnable,180000);
        super.onCreate();
    }

This task run continuously 3min

public void TASK()
{
     //your task 

}
Slash answered 7/4, 2017 at 6:44 Comment(0)
H
-8

Services were designed to run in the background, no need for a different process.

From the services documentation:

Once started, a service can run in the background indefinitely, even if the component that started it, is destroyed.

The service however, should stop itself, when its work is done. IntentService is the easiest way to start implementing a service.

Hispaniola answered 21/7, 2013 at 8:34 Comment(4)
Yeah I saw this line but still, every time when I close the app the service being distroyed also. By the way, I don't know if it matters but the service implement OnSensorListener to listen to accelometers changes(movements..)Deli
@powerX How do you start the service? Are you binding to it or with startService?Hispaniola
I'm using startService with intent in order to start my serviceDeli
@powerX Sorry, no idea then.Hispaniola

© 2022 - 2024 — McMap. All rights reserved.