我根据亲爱的mayank'answer编辑了我的代码,但是在方法开始之前,它没有显示任何作为displaymsg()方法的输入发送的消息..我应该说methodtest()是从nfc和onnewintent方法开始的(意图)

@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);
    }
}

在我的布局中:
<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>

最佳答案

记住,理想情况下,异步任务应该用于短操作(最多几秒钟)。
尝试了解更多关于asynctask的信息,您的代码中有很多错误
不要手动调用doInBackground()
dc.doInBackground(totalMsg);/错误
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);
    }
}

10-05 18:58