Sri*_*ram 11 java multithreading android android-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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我面临的问题:
1.unzipDest和index,和更新的值doInBackground保持为null Unzip及其所有对象.如何确保值保持更新?
2.我知道doInBackground发生在与主UI线程分开的线程中.这是否意味着一旦该线程返回,新线程中更新的任何值都将丢失?
cod*_*gic 14
如何确保值保持更新?
它们将被更新,因为它们是成员变量.但是,由于它AsyncTask是异步的,因此在检查时它们可能尚未更新.您可以interface在更新这些值时使用an 来创建回调.这个答案涵盖了如何做到这一点
这是否意味着一旦该线程返回,新线程中更新的任何值都将丢失?
不,他们不应该"迷失".它们可能只是AsyncTask在你检查时没有改变.
由于这不是您的实际代码,我在您尝试访问它们时无法看到,但您可以使用该interface方法或调用需要这些值的函数onPostExecute().您也可以null在尝试访问它们之前进行检查.它取决于您需要的功能和流程,哪种是最佳方式.希望有所帮助.
编辑
在我链接的答案中,您告诉Activity您将在创建单独的内容后使用它interface并implements AsyncResponse在Activity声明中覆盖其方法interface class
public class MainActivity implements AsyncResponse{
Run Code Online (Sandbox Code Playgroud)
然后,在你的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.
}
Run Code Online (Sandbox Code Playgroud)
然后onPostExecute()在监听器看到它完成的时候调用它是(你的接口类)的delegate.processFinish(result); delegate一个实例AsyncResponse
public class AasyncTask extends AsyncTask{
public AsyncResponse delegate=null;
@Override
protected void onPostExecute(String result) {
delegate.processFinish(result);
}
Run Code Online (Sandbox Code Playgroud)
Interface示例取自上面的链接答案,并为了清楚起见进行了调整/评论.所以,如果它对任何人有帮助,请务必提出答案.