簡體   English   中英

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

[英]Inner class can access but not update values - AsyncTask

我正在嘗試使用Android的AsyncTask解壓縮文件夾。 該類(稱為Decompress)是Unzip的內部類,其中Unzip本身是非Activity類。 偽代碼為:

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

               @Override
        protected Boolean doInBackground(Void... params) {
                       //unzip operation goes here.
                       unzipDest = something;  //unzip destination is set here.

                       if(unzip operation is successful) {
                          result = true;
                          index = url pointing to location of unzipped folder.
                       } else {
                         result = false;
                       }
                }

    @Override
        protected void onPostExecute(Boolean result) {
            if(result) {
                if(pd != null) {
                    pd.setTitle("Success");
                    pd.setMessage("folder is now ready for use.");
                    pd.show();
                    pd.dismiss();
                    pd = null;
                    Log.v(this.toString(), "Unzipped.");

                    index = unzipDest + "/someURL";
                    Log.v(this.toString(), "index present in: " + index);
                }
            } else {
                pd = ProgressDialog.show(mContext, "Failure", "Cannot unzip.");
                pd.dismiss();
            }
        }
    }   

我面臨的問題:
1.在doInBackground更新的unzipDestindex值對於Unzip及其所有對象保持為空。 如何確保這些值保持更新?
2.我知道doInBackground發生在與主UI線程分開的線程中。 這是否意味着一旦該線程返回,新線程中更新的所有值都會丟失?

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

由於它們是成員變量,因此將被更新。 但是,由於AsyncTask是異步的,因此當您檢查它們時可能尚未更新。 這些值更新后,您可以使用interface來創建回調。 這樣的答案涵蓋了如何做到這一點

這是否意味着一旦該線程返回,新線程中更新的所有值都會丟失?

不,他們不應該“迷路”。 在檢查它們時,它們可能只是未在AsyncTask進行更改。

由於這不是您的實際代碼,因此當您嘗試訪問它們時無法看到,但是您可以使用interface方法或在onPostExecute()調用需要這些值的函數。 您也可以在嘗試訪問它們之前進行null檢查。 最好取決於您所需的功能和流程。 希望能有所幫助。

編輯

在我鏈接的答案中,您告訴Activity您將使用該interface並在創建單獨的interface class后在Activity聲明中使用implements AsyncResponse覆蓋其方法。

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.
   }

然后,當偵聽器發現已使用delegate.processFinish(result);進行處理時,將在onPostExecute()調用此方法delegate.processFinish(result); delegateAsyncResponse的實例(您的接口類)

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

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

Interface示例摘自上面的鏈接答案,為清晰起見對其進行了調整/注釋。 因此,如果對任何人有幫助,請確保對該答案進行投票。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM