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 methodprotected void onHandleIntent(Intent intent)
in aIntentService
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();
}
}