Augusto
Augusto

Reputation: 4233

How keeping service when App is Killed instead restart?

I am using a service like as counter (0 - 10) for test the service, but when the app is killed, the service has restarted instead continue.

In the logcat shows:

04-05 17:25:47.497 25309-25309/? I/livro: onCreate
04-05 17:25:47.498 25309-25309/? I/livro: onStartCommand
04-05 17:25:48.531 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...0
04-05 17:25:49.571 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...1
04-05 17:25:50.611 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...2
04-05 17:25:51.651 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...3
04-05 17:25:52.692 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...4
04-05 17:25:53.733 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...5
04-05 17:25:54.771 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...6
04-05 17:25:55.811 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...7
04-05 17:25:56.851 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...8
04-05 17:25:57.891 25309-25325/com.example.augustoc.helloservice I/livro: HelloService executando...9
04-05 17:25:57.891 25309-25309/com.example.augustoc.helloservice I/livro: onDestroy

When the close app, the counter has restarted, but I want that service continue.

In the onStartCommand:

@Override                                               // ID DESTE SERVIÇO
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG,"onStartCommand");
        count = 0;
        running = true;

        //delega para a trhead
        new WorkerThread().start();
        return START_STICKY;
    }

I try return START_STICKY AND START_NOT_STICKY, both not works.

Upvotes: 1

Views: 86

Answers (1)

Remario
Remario

Reputation: 3863

use this constant when creating the service. replace return START_STICKY; with return START_REDELIVER_INTENT add your necessary logic. START_REDELIVER_INTENT If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand() with the last intent that was delivered to the service. Any pending intents are delivered in turn. This is suitable for services that are actively performing a job that should be immediately resumed.

Also please consider using JobScheduler,You will construct these JobInfo objects and pass them to the JobScheduler with schedule(JobInfo). When the criteria declared are met, the system will execute this job on your application's JobService. You identify which JobService is meant to execute the logic for your job when you create the JobInfo with JobInfo.Builder(int, android.content.ComponentName).

The framework will be intelligent about when you receive your callbacks, and attempt to batch and defer them as much as possible. Typically if you don't specify a deadline on your job, it can be run at any moment depending on the current state of the JobScheduler's internal queue, however it might be deferred as long as until the next time the device is connected to a power source.

Upvotes: 1

Related Questions