我根据Mayank的答案来编辑我的代码,但在displayMsg()方法开始之前没有显示任何已输入的消息..我应该说MethodTest()是用nfc启动的,在onNewIntent(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);
}
}
在我的布局中:
<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);
的意思是调用obj
对象的doInBackground(msg)
方法并将结果赋值给整型变量value
。请参考 AsyncTask 来正确实现 AsyncTask。 - ρяσѕρєя Kobj.execute("");
替换int value= obj.doInBackground(msg);
,并检查ProgressDialog
是否正在显示。 - ρяσѕρєя K