问题
I edited my code according dear Mayank'answer but It does not show any message that is sended as input in displayMsg() method before method begines..I should say MethodTest() is started with nfc and in method 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);
}
}
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>
回答1:
Remember that AsyncTasks should ideally be used for short operations (a few seconds at the most.)
Try to learn more about AsyncTask and there are so many mistakes in your code
Do not call
doInBackground()
manually.dc.doInBackground(totalMsg);
// ErrorDisplayMsg() called several times, each time a new instance of class DisplayMsgClass created
DisplayMsgClass dc = new DisplayMsgClass();
// ErroronPreExecute()
textView.setText("Hello !!!");
// NullPointerException.textView.setText()
is called without initializing it.
Caution
Do not call AsyncTask.execute()
more than one on a same intance.
For eg:
DisplayMsgClass displayMsgClass = new DisplayMsgClass();
displayMsgClass.execute();
displayMsgClass.execute(); //Error, IllegalStateException
will show you a basic demo based on you implementation and you can simply modify it according to your own way.
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);
}
}
回答2:
The reason why you are seeing onProgressUpdate(Progress...)
called at the end of if(...) is because publishProgress(Progress... values)
posts a message to an internal handler, this internal handler later processes the message to update the progress. In other words, publishProgress(Progress... values)
doesn't synchronously call onProgressUpdate(Progress...)
See implementation here: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/os/AsyncTask.java#L649
This is necessary because publishProgress(Progress... values)
is expected to be called from doInBackground(Params...)
, which is on the worker thread for an AsyncTask, whereas onProgressUpdate(Progress...)
happens on the UI thread so the UI can reflect the progress change. By posting a message to the handler on the UI thread, the progress info can be synced from worker thread to the UI thread without blocking either thread.
回答3:
Try below code
private int DisplayMsg(String msg) {
totalMsg += msg;
DisplayMsgClass dc = new DisplayMsgClass();
dc.runner.execute(totalMsg);
}
Hope it will work
:)GlbMP
回答4:
create progress bar in xml layout and set its visibility gone by default and create code as sample
// 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);
}
}
来源:https://stackoverflow.com/questions/40482166/progress-dialog-not-show-on-screen