内部类可以访问,但不更新值 - 的AsyncTask [英] Inner class can access but not update values - AsyncTask

查看:147
本文介绍了内部类可以访问,但不更新值 - 的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解压使用Android的的AsyncTask 文件夹。类(称为DECOM preSS)是一个内部类解压缩其中解压缩本身是一种非活性类。伪code是:

 公共类解压{
  私人字符串指数;
  私人字符串unzipDest; //用于存储文件夹的目标文件。
  私人活动的活动;
  私人布尔结果; //的DECOM preSS的结果。

  公共无效解压(字符串LOC){

    DECOM preSS workThread =新DECOM preSS(LOC,活动);
    workThread.execute();
    如果(解压缩操作成功){
      显示器(指数);
  }

  //类DECOM preSS:
类DECOM preSS延伸的AsyncTask<太虚,整型,布尔> {

        私人ProgressDialog PD = NULL;
        私人语境mContext;
                私人字符串禄;
        私人诠释的nentries;
        私人诠释entriesUnzipped;

        公共DECOM preSS(字符串位置,语境三){
                        LOC =位置;
            mContext = C;
            的nentries = 0;
            entriesUnzipped = 0;
            Log.v(this.toString(),退出DECOM preSS的构造。);
        }

        @覆盖
        在preExecute保护无效(){
            Log.v(this.toString(),里面有关于preExecute。);
            PD =新ProgressDialog(mContext);
            pd.setTitle(解压文件夹。);
            pd.setMessage(解压正在进行中。);
            pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            Log.v(this.toString()显示对话框,并退出。);
            pd.show();
        }

               @覆盖
        保护布尔doInBackground(空... PARAMS){
                       //解压操作放在这里。
                       unzipDest =东西; //解压缩的目标是在这里设置。

                       如果(解压缩操作成功){
                          结果=真;
                          指数= URL指向解压后的文件夹的位置。
                       } 其他 {
                         结果= FALSE;
                       }
                }

    @覆盖
        保护无效onPostExecute(布尔结果){
            如果(结果){
                如果(PD!= NULL){
                    pd.setTitle(成功);
                    pd.setMessage(文件夹现在已经可以使用了。);
                    pd.show();
                    pd.dismiss();
                    PD = NULL;
                    Log.v(this.toString(),解压缩);

                    指数= unzipDest +/ someURL;
                    Log.v(this.toString(),索引present:+指数);
                }
            } 其他 {
                PD = ProgressDialog.show(mContext,失败,无法解压缩。);
                pd.dismiss();
            }
        }
    }
 

问题我面对的:
1.值 unzipDest 首页 doInBackground ,依然为null,解压缩及其所有对象。我怎样才能确保值保持更新?
2.我知道doInBackground发生在一个线程独立于主UI线程。这是否意味着,在新线程更新的任何值都将丢失,一旦该线程的回报?

解决方案
  

我怎样才能确保值保持更新?

它们将被更新,因为它们是成员变量。然而,由于的AsyncTask 是 - 异步,他们可能暂时还无法更新,当您检查。您可以使用接口来创建一个回调时,这些值将被更新。 <一href="http://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a">This SO答案将介绍如何做到这一点

  

这是否意味着,在新线程更新的任何值将丢失一旦线程返回?

没有他们不应该被丢失。他们可能只是没有在改变的AsyncTask 当您检查它们。

由于这是不是你的实际code,当您试图访问他们,我看不到,但你可以使用接口方法或调用的函数需要在 onPostExecute这些值()。你也可以做一个在尝试进行访问前检查。这只是取决于你需要哪个是最好的方式的功能和流程。希望有所帮助。

修改

在回答我的联系,你告诉活动,你将使用接口并覆盖其方法(S)与实现AsyncResponse 活动声明创建单独的接口类

 公共类MainActivity实现AsyncResponse {
 

那么,在你的活动的是,你重写你的这个类(无效processFinish(字符串输出)声明的方法;

  @覆盖
 无效processFinish(字符串输出){//使用相同的PARAMS为onPostExecute()
 //这个你会收到结果从异步类onPostExecute(结果)的方法烧制。
   }
 

那么这就是所谓的 onPostExecute()当听众发现它与 delegate.processFinish(结果)完成的; 代理是一个实例 AsyncResponse (你的接口类)

 公共类AasyncTask扩展AsyncTask的{
公共AsyncResponse委托= NULL;

   @覆盖
   保护无效onPostExecute(字符串结果){
      delegate.processFinish(结果);
   }
 

接口例如,从上面链接的答案,并采取调整/注释清晰。所以一定要upvote的答案,如果它可以帮助任何人。

I am trying to unzip a folder using Android's AsyncTask. The class (called Decompress) is an inner class of Unzip where Unzip itself is a non-Activity class. The pseudo-code is:

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

Problems I am facing:
1. The value of unzipDest and index, updated in doInBackground, remain null to Unzip and all its objects. How can I ensure that the values remain updated?
2. I know that doInBackground occurs in a thread separate from the main UI thread. Does that mean that any values updated in the new thread will be lost once that thread returns?

解决方案

How can I ensure that the values remain updated?

They will be updated since they are member variables. However, since AsyncTask is asynchrounous, they might not be updated yet when you check them. You can use an interface to create a callback when these values are updated. This SO answer covers how to do this

Does that mean that any values updated in the new thread will be lost once that thread returns?

No they shouldn't be "lost". They probably just haven't been changed in the AsyncTask when you check them.

Since this isn't your actual code I can't see when you are trying to access them but you can use the interface method or call the functions that need these values in onPostExecute(). You also can do a null check before trying to access them. It just depends on the functionality and flow that you need as to which is the best way. Hope that helps.

Edit

In the answer I linked to, you tell the Activity that you will use that interface and override its method(s) with implements AsyncResponse in your Activity declaration after creating the separate interface class

public class MainActivity implements AsyncResponse{

then, in your Activity still, you override the method you declared in that class (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.
   }

then this is called in onPostExecute() when the listener sees that it is done with delegate.processFinish(result); delegate is an instance of AsyncResponse (your interface class)

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

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

Interface example taken from linked answer above and adjusted/commented for clarity. So be sure to upvote that answer if it helps anyone.

这篇关于内部类可以访问,但不更新值 - 的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆