2016-11-08 140 views
4

我編輯我的代碼,根據親愛的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> 
+3

你是什麼意思'int value = obj.doInBackground(msg);'?請參閱[AsyncTask](https://developer.android.com/reference/android/os/AsyncTask.html)以獲取正確的AsyncTask實現 –

+0

這並不重要,我把它放在確保DisplayMsg被執行並創建一個時間可以看到對話框 – Fatemeh

+5

好吧,使用'obj.execute(「」);'而不是'int value = obj.doInBackground(msg);'並檢查'ProgressDialog'是否顯示 –

回答

3

記住AsyncTasks應該理想地用於短操作(幾秒鐘之最。 )

嘗試瞭解有關AsyncTask的更多信息,並且您的許多錯誤代碼

  1. 請勿手動撥打doInBackground()

    dc.doInBackground(totalMsg); //錯誤

  2. DisplayMsg()調用多次,類DisplayMsgClass的新實例創建

    DisplayMsgClass dc = new DisplayMsgClass(); //錯誤

  3. onPreExecute()
    textView.setText("Hello !!!"); // NullPointerException異常各一次。在不初始化的情況下調用textView.setText()

注意

不要打電話超過一個AsyncTask.execute()更上了一個相同的intance。
對於如:

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

爲什麼你看到onProgressUpdate(Progress...)稱爲在IF(...)是最終的原因是因爲publishProgress(Progress... values)發佈消息到內部處理程序,這個內部處理程序稍後處理該消息更新進度。換句話說,publishProgress(Progress... values)不同步調用onProgressUpdate(Progress...) SEE實施這裏: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線程,而不會阻塞任何線程。

+0

謝謝你回答我。 wxcuse我我不明白。我應該在doInBackground(Params ...)中調用onProgressUpdate(Progress ...),而不是publishProgress(Progress ... values)?你可以給我代碼更改嗎? – Fatemeh

+0

我不清楚爲什麼你選擇使用AsyncTask:AsyncTask是設計用於在工作線程上運行的昂貴的後臺任務,以使UI線程免於執行可能導致丟幀的耗時的任務。 MethodTest()是否應該在UI線程或後臺線程上運行? –

+1

我使用nfc。它調用新的意圖方法,並在此方法中調用方法測試。我找不到任何解決方案並使用異步任務。如果你有我的問題的解決方案,請給我。我只想在調用方法之前顯示消息 – Fatemeh

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); 
    } 
} 
+0

謝謝你回答我。不好意思,什麼是textview?爲什麼使用?我編輯了我的代碼,包括您的帖子。但不會再顯示任何消息 – Fatemeh

+0

它的一個例子,所以基本上在這個例子中,我們從服務器(在json中)獲取數據並將其顯示到Textview中。 你必須根據你的要求修改它 –

+0

我編輯了我的代碼。我把文本視圖,我把我編輯的代碼再次質疑。在調用dc.doInBackground(totalMsg)後;在displaymsg()方法中。只有doInBackground被調用並返回,沒有文字視圖中顯示。我不應該在doInBackground方法中調用publishProgress? – Fatemeh

1

試試下面的代碼

private int DisplayMsg(String msg) { 
    totalMsg += msg; 
    DisplayMsgClass dc = new DisplayMsgClass(); 
    dc.runner.execute(totalMsg); 
} 

希望它會工作

:)GlbMP

+0

對不起,但在直流亞軍是不明確的。我寫了dc.onPostExecute(totalMsg);但是當textv.setText(result);在方法onPostExecute(字符串結果)中執行textview內容沒有改變:(爲什麼需要進度條?它是做什麼的? – Fatemeh