Pause running on an Android Service

Asked

Viewed 56 times

0

I am developing an Android app where there is a IntentService to read a file.

Each line of the file represents a record that must have a specific format according to the requirements of the application. A record is invalid, for example, if the number of characters is greater than a certain size.

I need that if during reading the file any invalid record is found, the execution is paused and a notification is displayed to ask if he wants to skip that line and continue reading from the next line or if he wants to cancel all reading.

My only question is: How can I pause the execution of the code within a while in the method protected void onHandleIntent(Intent intent) in a IntentService to display the notification, if possible?

Sample code:

public class ImportImovelService extends IntentService {

    {...}

    // Será chamado assincronamente pelo Android
    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent == null)
            return;

            fis = new FileInputStream(file);
            br = new BufferedReader(new InputStreamReader(fis, FileUtil.ENCODE));

            String strLine;
            boolean eof = false;

            do {

                if ((strLine = br.readLine()) == null) {
                        // End of file (eof)
                        eof = true;
                        break;
                    }

                    if (strLine.length() > 136) {

                        // foi encontrado um registro inválido, deve pausar a
                        // leitura do arquivo e perguntar ao usuário se deve 
                        // continuar a leitura do arquivo.
                        // O usuário seleciona se continua ou se encerra a 
                        // leitura

                        if (usuarioEscolheContinuar) {
                            // pula o registro, cancela a iteração atual e
                            // prossegue para a próxima iteração
                            continue;
                        } else {
                            // a execução inteira é cancelada
                            stopForeground(true);
                            stopSelf();
                            return;                         
                        }

                    }


                // leitura normal do arquivo
                {...}                

            } while (!eof);

            stopForeground(true);
            stopSelf();
    }
}

1 answer

0

Whoa, that’s all right!?

Maybe the CountDownTimer may help you.

 new CountDownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {
         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     }

     public void onFinish() {
         mTextField.setText("done!");
     }
  }.start();

Create a countdown, and in case the user gives ok, you can cancel the count regressive and continue his instruction.

Browser other questions tagged

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