Run Android service indefinitely

0

Good afternoon, I have a question with my Android code, I was checking questions that other people have made on this page and everyone asks about running the task in the background or running despite closing the application, but my question is How can I get the service to run repeatedly until I send the onDestroy call?

I'm currently running it with a cycle for but this only executes the task until the account ends and then closes it, and try at the end of for subtract 1 from the variable condition, but then the application sends me an error and it closes.

Any ideas?

This is the code for my service:

public class ServicioBuscador extends Service {

    int a=0,calificando=0;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        for(int x=1;x<=20;x++)
        {
            Toast.makeText(ServicioBuscador.this, "aun no: "+x , Toast.LENGTH_SHORT).show();
            BuscarA();
        }

        return START_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void BuscarA(){
        if(a<=20){
                Toast.makeText(ServicioBuscador.this, "Contar: "+a , Toast.LENGTH_SHORT).show();
            UnSegundo();
                a=a+1;
        }else{
            NotificacionPositiva();
        }

    }
    private void UnSegundo(){
        try{
            Thread.sleep(1000);
        }catch (InterruptedException e){}
    }

But in this way the service only runs for 20 seconds and at the end of counting, or when a = 20 the service stops running.

    
asked by karasu55 07.11.2017 в 00:11
source

0 answers