How to show message that the app is processing

0

I have a method that is exponentially time complexity and it takes a while to process, a matter of 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 to use Toast before starting the method, but it only appears after.

    
asked by anonymous 27.02.2018 / 17:01

1 answer

3

Answering your question:

Correct is to use an extended class of AsyncTask to display a progress bar or an image, or whatever you want, and render in background .

In addition, because of the runtime, as raised by @ramaral (a user who knows Android too), I created the topic: When using AsyncTask, ThreadPoolExecutor, or Service

Why use it?

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

A quick and brief explanation of the AsyncTask class:

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

Main class methods:

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

doInBackground : will execute your task;

onProgressUpdate : will execute during the execution of your task;

onPostExecute : will execute after the task finishes.

Example of a% horizontal% with AsyncTask class:

Class progressBar :

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 CalculoMatematico :

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 Main

<?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 to reading:

AsyncTask Official Documentation

Example of using AsyncTask

    
27.02.2018 / 17:10