亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

內部類可以訪問但不能更新值-AsyncTask

內部類可以訪問但不能更新值-AsyncTask

MMTTMM 2019-06-19 14:53:02
內部類可以訪問但不能更新值-AsyncTask我正在嘗試用Android的AsyncTask..類(稱為解壓縮)是Unzip其中解壓縮本身是一個非活動類。偽碼是:public class Unzip {     private String index;     private String unzipDest;    //destination file for storing folder.   private Activity activity;   private boolean result;      //result of decompress.   public void unzip(String loc) {     Decompress workThread = new Decompress(loc, activity);     workThread.execute();       if(unzip operation was successful) {       display(index);   }   //Class Decompress:class Decompress extends AsyncTask<Void, Integer, Boolean> {         private ProgressDialog pd = null;         private Context mContext;                 private String loc;         private int nEntries;         private int entriesUnzipped;         public Decompress(String location, Context c) {                         loc = location;             mContext = c;             nEntries = 0;             entriesUnzipped = 0;             Log.v(this.toString(), "Exiting decompress constructor.");         }         @Override         protected void onPreExecute() {             Log.v(this.toString(), "Inside onPreExecute.");             pd = new ProgressDialog(mContext);             pd.setTitle("Unzipping folder.");             pd.setMessage("Unzip in progress.");             pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);             Log.v(this.toString(), "Showing dialog and exiting.");             pd.show();         }我所面臨的問題:1.的價值unzipDest和index,更新到doInBackground,保持空到Unzip以及它所有的目標。如何確保這些值保持更新?2.我知道doInBackplace發生在一個與主UI線程分離的線程中。這是否意味著,一旦線程返回,新線程中更新的任何值都將丟失嗎?
查看完整描述

3 回答

?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

如何確保這些值保持更新?

它們將被更新,因為它們是成員變量。然而,因為AsyncTask是異步的,當您檢查它們時,它們可能還沒有被更新。您可以使用interface若要在更新這些值時創建回調,請執行以下操作。這個答案涵蓋了如何做到這一點。

這是否意味著,一旦線程返回,新線程中更新的任何值都將丟失嗎?

不,他們不應該“迷路”。他們可能只是沒有改變AsyncTask當你檢查他們的時候。

由于這不是您的實際代碼,所以當您試圖訪問它們時,我看不到,但是您可以使用interface方法中需要這些值的函數。onPostExecute()..你也可以做一個null在嘗試訪問之前檢查一下。這取決于您所需要的功能和流程,而哪種方式是最好的。希望能幫上忙。

編輯

在我鏈接到的答案中,你告訴Activity你會用這個interface并用以下方法覆蓋它的方法:implements AsyncResponse在你的Activity在創建單獨的interface class

public class MainActivity implements AsyncResponse{

然后,在你的Activity盡管如此,您仍然重寫了在該類中聲明的方法(void processFinish(String output);)

@Override
 void processFinish(String output){  // using same params as onPostExecute()
 //this you will received result fired from async class of onPostExecute(result) method.
   }

然后調用這個onPostExecute()當偵聽器看到它已經完成時delegate.processFinish(result);delegateAsyncResponse(您的接口類)

    public class AasyncTask extends AsyncTask{public AsyncResponse delegate=null;

   @Override
   protected void onPostExecute(String result) {
      delegate.processFinish(result);
   }

Interface從上面的鏈接答案中摘取的例子,并為清晰起見調整/評論。所以,如果這個答案對任何人都有幫助的話,一定要把它提出來。


查看完整回答
反對 回復 2019-06-19
?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

我希望能夠訪問其url存儲在其中的文件。index在成功完成解壓縮操作之后,workThread.execute()..在你提到的問題中,我不明白界面是怎么回事AsyncResponse被初始化了。你能解釋一下嗎?謝謝

查看完整回答
反對 回復 2019-06-19
  • 3 回答
  • 0 關注
  • 461 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號