如何从AsyncTask显示ProgressDialog,它从doInBackground()返回布尔值?

时间:2014-11-26 12:28:49

标签: android android-asynctask progressdialog

我正在调用AsyncTask进行登录的活动。 如果它返回true,则切换到另一个活动。

signIn.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Log.d("On Click Sign In", "Before PD shown");
        //          pd.show();

        Log.d("On Click Sign In", "After PD shown");
        try {
            if (new SignIn(SessionLogin.this).execute(email, pass).get())
            {
                Log.d("onClick", "True");
                Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
                Intent i = new Intent(SessionLogin.this, DashBoardMain.class);
                //              pd.dismiss();
                Log.d("SessionLogin", "true SignIn");
                startActivity(i);
                finish();
            }
            else 
            {
                Toast.makeText(SessionLogin.this, "Sorry! Something went wrong. Please try again.", Toast.LENGTH_LONG).show();
                Intent i = new Intent(SessionLogin.this, Woftos_sign_in_up.class);
                //          pd.dismiss();
                Log.d("SessionLogin", "false || Null SignIn");
                startActivity(i);
                finish();
            }

            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ExecutionException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

});

我正在连接数据库并返回值的AsyncTask。 直到asynctask运行,我想显示ProgressDialog。

public class SignIn extends AsyncTask<String, Void, Boolean> {

    String queryString;

    ProgressDialog PD;
    public static String NodeID=null;
    public static String Gender=null;
    public static String imageName=null;
    String result;
    Context context;

    public SignIn(Context mcontext){
        context=mcontext;
        Log.d("constructor", "context "+context.toString());
        PD=new ProgressDialog(mcontext);
        PD.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        PD.setTitle("Please Wait...");
        PD.setMessage("Signing in...");
        PD.setCancelable(false);
        PD.setIndeterminate(true);
        PD.show();
    }

    protected void onPostExecute(Boolean x) {   
    super.onPostExecute(x);
    if(PD.isShowing()){
        Log.d("onPostExecute", "ProgressDialog b4 dismiss");
        PD.dismiss();
        Log.d("onPostExecute", "ProgressDialog after dismiss");
        }
    }

    @Override
    protected Boolean doInBackground(String... arg0) {

        Log.d("in async", "" + arg0[0] + " " + arg0[1]);


            try {
                //Connecting to database
                DBConnect.connect();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            queryString = "{\"query\":\"START n=node(*) WHERE n.Email_Id='"
                    + arg0[0] + "' AND n.Password='" + arg0[1]
                    + "' RETURN n.Password,id(n),n.Gender,n.Profile_Photo\", \"params\":{}}";

            try {
                //Sending Query
                DBConnect.sendQuery(queryString);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            try {
                //Getting result of Query
             result = DBConnect.readInputStream();

            JSONObject obj1 = new JSONObject(result);
            JSONArray nameArray = obj1.getJSONArray("data");
            int res=nameArray.length();
            if(res==0)
            {
                return false;
            }
            result = nameArray.getJSONArray(0).getString(0);
            NodeID = nameArray.getJSONArray(0).getString(1);
            Gender = nameArray.getJSONArray(0).getString(2);
            //imageName = nameArray.getJSONArray(0).getString(3);

            Log.d("NodeID-Gender in SignIn", ""+result+" " + NodeID+" "+Gender);
            DashBoardMain.nodeID=NodeID;
            DashBoardMain.DBGender=Gender;
            //DashBoardMain.imageName=imageName;
            return true;
            //DashBoardMain.imageName=imageName;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;

    }
}

无论我做什么,ProgressDialog都不会出现。其余的代码可以很好地连接数据库并返回值。 请帮忙..

4 个答案:

答案 0 :(得分:0)

在asynctask中尝试这样,

@Override
    protected void onPreExecute() {
       PD=new ProgressDialog(mcontext);
    PD.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    PD.setTitle("Please Wait...");
    PD.setMessage("Signing in...");
    PD.setCancelable(false);
    PD.setIndeterminate(true);
    PD.show();
        super.onPreExecute();
    }

 @Override
    protected void onPostExecute(String[] result) {
        PD.dismiss();
        super.onPostExecute(result);
    }

答案 1 :(得分:0)

尝试使用这样的pre excecute

public SignIn(Context mcontext){
    context=mcontext;
    Log.d("constructor", "context "+context.toString());
}

protected void onPreExecute() {
    super.onPreExecute();
    PD=new ProgressDialog(context);
    PD.setTitle("Please Wait...");
    PD.setMessage("Signing in...");
    PD.setCancelable(false);
    PD.setIndeterminate(true);
    PD.show();
        }

或--------------- 这可能是因为您的流程突然完成所以progressdialog可能会突然关闭。要避免这种情况发生在您的身上post execute尝试:

protected void onPostExecute(Boolean x) {   
super.onPostExecute(x);
if(PD.isShowing()){
    Log.d("onPostExecute", "ProgressDialog b4 dismiss");
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
    public void run() {
         PD.dismiss();
    }}, 3000);

    Log.d("onPostExecute", "ProgressDialog after dismiss");
    }
} 

或------------------------

从aynctask调用中删除.get()get()等待计算完成所需,然后检索其结果。这意味着,您正在阻止您的UI线程,等待结果。

解决方案:不要打电话给

通常,您将在postExecute中调用函数(回调)。

答案 2 :(得分:0)

Override the OnPreExecute() of AsyncTask and paste

context=mcontext;
Log.d("constructor", "context "+context.toString());
PD=new ProgressDialog(mcontext);
PD.setProgressStyle(ProgressDialog.STYLE_SPINNER);
PD.setTitle("Please Wait...");
PD.setMessage("Signing in...");
PD.setCancelable(false);
PD.setIndeterminate(true);
PD.show();

答案 3 :(得分:0)

在onPreExecute()方法中显示你的ProgressDialog,并在AsyncTask的onPostExecute()方法中将其关闭..就像这样。

protected void onPreExecute() {


 PD=new ProgressDialog(mcontext);
    PD.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    PD.setTitle("Please Wait...");
    PD.setMessage("Signing in...");
    PD.setCancelable(false);
    PD.setIndeterminate(true);
    PD.show();

};

protected void onPostExecute(Boolean x) {   
super.onPostExecute(x);
if(PD.isShowing()){
    Log.d("onPostExecute", "ProgressDialog b4 dismiss");
    PD.dismiss();
    Log.d("onPostExecute", "ProgressDialog after dismiss");
    }
}