Update ProgressBar in the background java android

1

How do I make the background service update ProgressBar on Android ?

My code:

public void onClick(View v) {
    // inicial o progressbar
            progressBar = new ProgressDialog(v.getContext());
            progressBar.setCancelable(true);
            progressBar.setMessage("Exportando os dados...");
            progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressBar.setProgress(0);
            progressBar.setMax(100);
            progressBar.show();

// Inicia o serviço a partir de INICIAR_SERVICO definido no

    startService(new Intent("INICIAR_SERVICO"));

I'm not sure how to use progressBar.setProgress(); in the called service class.

    
asked by anonymous 20.04.2014 / 03:06

1 answer

0

The service can not call progressBar.setProgress() directly. It needs to figure out how to request the Activity visible at that time that this method is called.

I see two possibilities: the first, which is not my favorite because it is a bit more complicated, is to bind to the service and thus allow the service to communicate with the Activity in question. For this, the reference to a listener that the service calls each time the progress must be increased must be entered into the service. In Activity , listener executes progressBar.setProgress() . The documentation explains how to connect to a service .

The second, which I prefer, is to broadcast updates from progress via broadcast. You record a BroadcastReceiver associated with the life cycle of your Activity (for example, registerReceiver() is in onResume() and unregisterReceiver() in onPause() - my doubt here, I'm not sure if the best methods for this are the onResume()/onPause() or onStart()/onStop() ). And when Activity gets a broadcast intent containing the new value of progress , it calls progressBar.setProgress() .

Code you can rely on:

MyActivity.java:

public class MinhaActivity extends Activity {

    private BroadcastReceiver mReceiver;
    private IntentFilter mFilter;

protected void onCreate(Bundle savedInstanceState) {
    mFilter = new IntentFilter();
    mFilter.addAction("ATUALIZA_PROGRESSO");
    mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("ATUALIZA_PROGRESSO".equals(intent.getAction())) {
                mProgressBar.setProgress(intent.getIntExtra("progress"));
                if (intent.getIntExtra("progress") >= 100) {
                    mProgressBar.dismiss();
                }
            }
        }
    }

    mProgressBar = new ProgressDialog(v.getContext());
    mProgressBar.setCancelable(true);
    mProgressBar.setMessage("Exportando os dados...");
    mProgressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    mProgressBar.setProgress(0);
    mProgressBar.setMax(100);
}

public void onClick(View v) {
    // inicia o progressbar
    mProgressBar.show();

    // Inicia o serviço
    startService(new Intent(this, MeuServico.class));
}

protected void onResume() {
    registerReceiver(mReceiver, mFilter);
}

protected void onPause() {
    unregisterReceiver(mReceiver);
}

MyService.java

public class MeuService extends IntentService {
    protected void onHandleIntent(Intent intent) {
        while (tarefaNaoTerminada) {
            (...)
            Intent intentAEnviar = new Intent("ATUALIZA_PROGRESSO");
            intentAEnviar.putExtra("progress", progresso);
            sendBroadcast(intentAEnviar);
        }
    }
}
    
20.04.2014 / 23:35