How to display message that the app is processing

Asked

Viewed 291 times

0

I have a method that is exponential time complexity and it takes a while To process, thirty seconds on average. However, during this time, the app hangs and gives no sign of life. I wanted to show a wait message, I tried using Toast before starting the method, but it only appears after.

  • The correct thing is to use an Asynctask class, and for that you want to use the methods of the same.

  • vlw, I’ll find out more about this class

  • @RBZ Asynctask is not suitable for such time-consuming processing. In this case Executor, Threadpoolexecutor, Futuretask or a Service should be used.

  • @ramaral I’m going to open a topic on this, it’s interesting. Thanks again ! Link: https://answall.com/questions/279330/quando-usar-asynctask-executor-threadpoolexecutor-futuretask-ou-um-service

1 answer

3


Answering your question:

The correct is to use an extended class of AsyncTask to display a progress bar or an image, or whatever, and process in background.

Also, because of the running time, as raised by @ramaral (a user who knows too much Android), I created the topic: When to use Asynctask, Threadpoolexecutor or Service

Why use it ?

Basically because Android after few seconds of processing, it can recognize that your APP has stopped, so it creates an asynchronous class that runs background.

A brief and brief explanation of the Asynctask class:

Class properties: AsyncTask<Parâmetros,Progresso,Resultado>

Main methods of the class:

onPreExecute: will run before starting the task (doInBackground);

doInBackground: will perform your task;

onProgressUpdate: will execute during the execution of your task;

onPostExecute: will perform after the task ends.

Example of a progressBar horizontal with Asynctask class:

Class CalculoMatematico:

public class CalculoMatematico extends AsyncTask<Void, Integer, String> {

    Context ctx;
    ProgressBar progressBar;

    public CalculoMatematico(Context ctx, ProgressBar progressBar) {
        this.ctx = ctx;
        this.progressBar = progressBar;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        this.progressBar.setProgress(0);
        this.progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        this.progressBar.setVisibility(View.GONE);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        this.progressBar.setProgress(values[0]);
    }

    @Override
    protected String doInBackground(Void... params) {

        // Seu código aqui


        // Aqui você seta o valor máximo
        this.progressBar.setMax(valor);

        // Aqui publica o valor atual, conforme atualiza
        publishProgress(valor);

    }

Class Main:

public class Main extends AppCompatActivity {

    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.progressBar = findViewById(R.id.progressBarId);
        // Aqui você esconder sua progressBar ao iniciar ou fazer isso direto no xml
        this.progressBar.setVisibility(View.GONE);
    }

    public void calcular() {
        CalculoMatematico calculo = new CalculoMatematico(this, this.progressBar);
        calculo.execute();
    }

XML activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.exemplo.recebejsonsqlite.Main">


    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="30dp"
        android:progress="0"
        android:progressTint="@android:color/holo_blue_dark" />

        ...Seus outros elementros...

</LinearLayout>

References for reading:

Official Documentation Asynctask

Example of Asynctask usage

Browser other questions tagged

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