我編輯我的代碼,根據親愛的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>
你是什麼意思'int value = obj.doInBackground(msg);'?請參閱[AsyncTask](https://developer.android.com/reference/android/os/AsyncTask.html)以獲取正確的AsyncTask實現 –
這並不重要,我把它放在確保DisplayMsg被執行並創建一個時間可以看到對話框 – Fatemeh
好吧,使用'obj.execute(「」);'而不是'int value = obj.doInBackground(msg);'並檢查'ProgressDialog'是否顯示 –