java 带有 ProgressDialog 的外部 AsyncTask 类 [更新:并返回?]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3347247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:25:06  来源:igfitidea点击:

External AsyncTask class with ProgressDialog [Update: and returning back?]

javaandroidandroid-asynctaskprogressdialog

提问by droidgren

**Updated: (See below)**I have been looking around for couple of days and can't find a straight answer to this. Some say it possible to some say to accomplish some say it's not. I am getting crazy on this.

**更新:(见下文)**我已经环顾了几天,找不到直接的答案。有人说可以有人说可以完成有人说不能。我对这个越来越疯狂了。

What I want is just to have the AsyncTaskTask showing a progressbar an external class. To do this I am passing the context as you can see in the main class. But whatever I try I get NullPointerException.

我想要的只是让 AsyncTaskTask 显示一个外部类的进度条。为此,我正在传递上下文,正如您在主类中看到的那样。但无论我尝试什么,我都会得到NullPointerException

Working code examples is appreciated. Thank you

工作代码示例表示赞赏。谢谢

Using Android 2.2 by the way.

顺便说一下,使用 Android 2.2。

main:

主要的:

import android.app.Activity;
import android.os.Bundle;

public class AsyncDemo extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new AsyncClass(this).execute();
    }
}

AsyncClass.java

异步类

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.SystemClock;

public class AsyncClass extends AsyncTask<Void, String, Void> {
    private Context context;
    ProgressDialog dialog = new ProgressDialog(context);

    public AsyncClass(Context cxt) {
        context = cxt;
    }

    @Override
    protected void onPreExecute() {
        dialog.setTitle("Please wait");
        dialog.show();
    }

    @Override
    protected Void doInBackground(Void... unused) {
        SystemClock.sleep(2000);
        return (null);
    }

    @Override
    protected void onPostExecute(Void unused) {
        dialog.dismiss();
    }
}

Update:I have a follow up question: Using the above code, is it possible to return a value from the onPostExecute method back to the main class, somehow? (Sorry about beeing noobish) I have tried something like this:

更新:我有一个后续问题:使用上面的代码,是否可以以某种方式将 onPostExecute 方法中的值返回到主类?(对不起,我太笨了)我试过这样的事情:

String result = new AsyncClass(this).execute();

and then a method that return back a string. But I can't do that because i got:

然后是一个返回字符串的方法。但我不能这样做,因为我得到了:

Type mismatch: cannot convert from AsyncTask<String,Void,Void> to String

What can I do to tackle this problem? Thanks.

我能做些什么来解决这个问题?谢谢。

回答by SSZero

You were creating the ProgressDialogwith a null context. The following code worked for me.

您正在ProgressDialog使用空上下文创建。以下代码对我有用。

public class AsyncClass extends AsyncTask<Void, String, Void> {
    private Context context;
    ProgressDialog dialog;

        public AsyncClass(Context cxt) {
            context = cxt;
            dialog = new ProgressDialog(context);
        }

        @Override
        protected void onPreExecute() {
            dialog.setTitle("Please wait");
            dialog.show();
        }

        @Override
        protected Void doInBackground(Void... unused) {
            SystemClock.sleep(2000);
            return (null);
        }

        @Override
        protected void onPostExecute(Void unused) {
            dialog.dismiss();
        }
    }

回答by SMR

ok this is what I did as i was using fragments. This is how you call the AsyncTask inside a fragment:

好的,这就是我在使用片段时所做的。这是您在片段中调用 AsyncTask 的方式:

String result=new AsyncClass(getActivity()).execute();

and this is how my AsyncTask outer class looks like:

这就是我的 AsyncTask 外部类的样子:

public class AsyncClass extends AsyncTask<Void, Void, String> {

    ProgressDialog pdialog;

    public AsyncClass(Context context) {
        pdialog = new ProgressDialog(context);
    }

    @Override
    protected void onPreExecute() {
        pdialog.setIndeterminate(true);
        pdialog.setCancelable(false);
        pdialog.setTitle("Loading Feed..");
        pdialog.setMessage("Please wait.");pdialog.show();
    }

    @Override
    protected String doInBackground(Void... params) {
        String result=null;
        //do your task here and generate result String
        return result;
    }

    @Override
    protected void onPostExecute(String result) {
        if(pdialog.isShowing())
             pdialog.dismiss();
    }
}

@SSZerothanks great answer, helped a lot.

@ SSZero感谢伟大的回答,帮了很多忙。

回答by rishu

I would like to answer that follow up question i.e.

我想回答那个后续问题,即

I have a follow up question: Using the above code, is it possible to return a value from the onPostExecute method back to the main class, somehow? (Sorry about beeing noobish) I have tried something like this:

String result = new AsyncClass(this).execute();

我有一个后续问题:使用上面的代码,是否可以以某种方式将 onPostExecute 方法的值返回到主类?(对不起,我太笨了)我试过这样的事情:

String result = new AsyncClass(this).execute();

I did this in my code it worked.

我在我的代码中做到了这一点。

AsyncClass ac=new AsyncClass();

ac.execute("");

String rslt=ac.get();

Call this code wherever you want to.

随时随地调用此代码。

public class AsynchClass extends AsyncTask <String,Integer,String>
{

    public String result=null;


    @Override
    protected void onPreExecute() {
    // TODO Auto-generated method stub  

    }

    protected String doInBackground(String... params) 
    {

            // Do all your background task here

            return result;

    }
    @Override

    protected void onProgressUpdate(Integer... values) {


    }

    protected void onPostExecute(String result) {

    }
}