How do I maintain and display Countdown when leaving the app?

Asked

Viewed 109 times

0

I’m building an app to run tasks every hour, and I want to show countdown. But when I exit the app, when I return, the countdown restarts. I need that when I return to the app, the countdown is continuing. Can someone help me ?

  • for this, Voce will need to create a service to run in the background and set this service as boot

  • @Armandomarquessobrinho could post an example?

  • another alternative would be to save in the database the time the service will take to be completed and the time the person left the app, recommend reading on Life Cycle of an Activity and then when the user opened the app, he calculated the time difference (output and input) and updated the remaining hours.

1 answer

1


Okay, come on then:

first, Voce needs to initialize the Service:

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class ServicoIniciadoNoBoot extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // Algo para ser feito quando o serviço for criado
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        // algo que precisa ser feito quando o serviço for incializado
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // Algo que precisa ser feito quando o serviço for finalizado (destruido)
    }
}

Then you will need a Broadcastreceiver, in which there is the onReceive method that will be called at the conclusion of the boot event. In it, we will launch the Service we have just created.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class BootCompletadoReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
         Intent serviceIntent = new Intent(context, ServicoIniciadoNoBoot.class);
         context.startService(serviceIntent);
     }
}

Then, Voce needs to modify the Androidmanifest.xml file in order for the app to respond to the service:

1) Add permission to capture the charging event on the device boot:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

2) register the Service:

<service android:name=".ServicoIniciadoNoBoot" ></service>

3) Register Broadcastreceiver to receive the event:

<receiver
    android:name=".BootCompletadoReceiver"
    android:enabled="true"
    android:exported="false" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

With these steps Oce creates a service running in your app where every time it is started by the app it keeps running until the device completes the booot, IE, is off.

You were due to what you have done there in code, so it was only possible to be Generic, but I believe that in onStart and onCreate of Servico Voce can manipulate your watch.

I hope it helps!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.