我根据亲爱的Mayank编辑了我的代码'回答但它没有显示在方法开始之前在displayMsg()方法中作为输入发送的任何消息。我应该说使用nfc和方法onNewIntent启动MethodTest() (意图)
@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>
答案 0 :(得分:3)
请记住,理想情况下,AsyncTasks应该用于短操作(最多几秒钟。)
尝试了解有关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);
}
}
答案 1 :(得分:2)
您在if(...)结尾处看到调用onProgressUpdate(Progress...)
的原因是因为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...)
发生在 UI线程,以便UI可以反映进度更改。通过在UI线程上向处理程序发布消息,可以将进度信息从工作线程同步到UI线程,而不会阻塞任一线程。
答案 2 :(得分:1)
尝试以下代码
private int DisplayMsg(String msg) {
totalMsg += msg;
DisplayMsgClass dc = new DisplayMsgClass();
dc.runner.execute(totalMsg);
}
希望它能运作
:) GlbMP
答案 3 :(得分:0)
在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);
}
}