Progress Dialog Does Not Display On Screen

I edited my code according to the respected Mayank'answer, but it does not display a message that is sent as input in the displayMsg () method before the method starts. I have to say that the MethodTest () method is started using nfc and in the onNewIntent method (Intent intent)

@Override
protected void onNewIntent(Intent intent) {
   MethodTest();
    ..............

}

public void MethodTest() {
    DisplayMsg("method 1 is running");
    Method1();

    DisplayMsg("method 2 is running");
    Method2();

    DisplayMsg("method 3 is running");
    Method3();

}

private int DisplayMsg(String msg) {
    totalMsg += msg;
    DisplayMsgClass dc = new DisplayMsgClass();
    dc.doInBackground(totalMsg);
}

private class DisplayMsgClass extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
         textView.setText("Hello !!!");
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        progressBar.setVisibility(View.VISIBLE);
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

    }

    @Override
    protected String doInBackground(String... Messages) {


        return Messages[0];
    }

    @Override
    protected void onPostExecute(String result) {
        progressBar.setVisibility(View.INVISIBLE);

        textView.setText(result);
    }
}

in my layout:

<LinearLayout>
<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:id="@+id/progressBar1"
    />
 <TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/textv1"
  android:hint="AppletPass"
  android:gravity="center"/>
 </LinearLayout>
+4
source share
4 answers

Remember that AsyncTasks should ideally be used for short operations (no more than a few seconds).

Try to learn more about AsyncTask and there are so many errors in your code.

  • Do not call doInBackground()manually.

    dc.doInBackground(totalMsg);//Error

  • DisplayMsg() , , DisplayMsgClass

    DisplayMsgClass dc = new DisplayMsgClass();//

  • onPreExecute()
    textView.setText("Hello !!!");//NullPointerException. textView.setText() .

AsyncTask.execute() .
,

DisplayMsgClass displayMsgClass = new DisplayMsgClass();  
displayMsgClass.execute();  
displayMsgClass.execute(); //Error, IllegalStateException  

, , .

public void MethodTest() {

    // execute task
    new DisplayMsgClass().execute("Download now");
}

/*
public void MethodTest() {
    DisplayMsg("method 1 is running");
    Method1();

    DisplayMsg("method 2 is running");
    Method2();

    DisplayMsg("method 3 is running");
    Method3();

}

private int DisplayMsg(String msg) {
    totalMsg += msg;
    DisplayMsgClass dc = new DisplayMsgClass();
    dc.doInBackground(totalMsg);
}
*/

private class DisplayMsgClass extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        // retrieve the widgets
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        textView = (TextView) findViewById(R.id.textv1);


        textView.setText("Download initialized");
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected String doInBackground(String... Messages) {

        // read commands
        String command = Messages[0];
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "Download completed";
    }

    @Override
    protected void onPostExecute(String result) {

        //invoked on the UI thread after the background computation finishes
        progressBar.setVisibility(View.INVISIBLE);
        textView.setText(result);
    }
} 
+3

, onProgressUpdate(Progress...), if (...), publishProgress(Progress... values) , . , publishProgress(Progress... values) onProgressUpdate(Progress...) . : https://github.com/android/platform_frameworks_base/blob/master/core/java/android/os/AsyncTask.java#L649

, , publishProgress(Progress... values) doInBackground(Params...), AsyncTask, onProgressUpdate(Progress...) , . , .

+2

private int DisplayMsg(String msg) {
    totalMsg += msg;
    DisplayMsgClass dc = new DisplayMsgClass();
    dc.runner.execute(totalMsg);
}

,

:) GlbMP

+1

xml

//AsyncTask.

    private class DownloadWebPageTask extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        //textView.setText("Hello !!!");
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);
        progressBar.setVisibility(View.VISIBLE);
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

    }

    @Override
    protected String doInBackground(String... urls) {
        String response = "";
        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();

                BufferedReader buffer = new BufferedReader(new InputStreamReader(
                        content));
                String s = "";
                while ((s = buffer.readLine()) != null) {
                    response += s;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        progressBar.setVisibility(View.INVISIBLE);
        textView.setText(result);
    }
}
0

Source: https://habr.com/ru/post/1660156/


All Articles