来自另一个 Activity 的异步任务中的进度对话框

ProgressDailog in asynctask from another Activity

我有两个Activity。 MainActivity 正在调用扩展 AsyncTask 的另一个 Activity。在AsyncTask中我显示了ProgressDailogonPreExecute(),但是ProgressDailog没有显示。原因可能是 AyncTask 执行时间较短,但响应需要时间,因此应显示 ProgressDialog

提供以下代码。

艾森克class

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

        Context context;
        String[] name,value;
        String Geturl,para;
        ProgressDialog progressDialog;

        NewConnections(Context context,String[] name,String[] value,String Url){
            this.context = context;
            this.name = name;
            this.value = value;
            Geturl = Url;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(context);
            progressDialog.setMessage("Please Wait");
            progressDialog.show();
        }
        @Override
        protected String doInBackground(Void... voids) {
            try
            {
                para = DCBUtil.jsonCovertion(name,value);
                URL url = new URL(Geturl);

                String responseStr = XXX.callJsonHttp(url,para);
                System.out.println("Response of Home Open FD :: "+responseStr);

                return responseStr;

            }catch(Exception e)
            {
                return "Error";
            }
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            progressDialog.dismiss();

        }
    }

主类

public class FirstPage extends AppCompatActivity {

    Button existUser,newUser;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_page);

        existUser = (Button)findViewById(R.id.existUser);
        newUser = (Button)findViewById(R.id.newUser);

        newUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String[] value = {"asdasd"};
                String[] name = {"dprm"};

                try {
                    NewConnections v= new NewConnections(FirstPage.this,name,value,"url is provided");
                    v.execute().get();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

试试这个:

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

        Context context;
        String[] name,value;
        String Geturl,para;
        public static ProgressDialog progressDialog;

        NewConnections(Context context,String[] name,String[] value,String Url){
            this.context = context;
            this.name = name;
            this.value = value;
            Geturl = Url;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(context);
            progressDialog.setCancelable(false);
            progressDialog.setMessage("Please Wait");
            progressDialog.show();
        }
        @Override
        protected String doInBackground(Void... voids) {
            try
            {
                para = DCBUtil.jsonCovertion(name,value);
                URL url = new URL(Geturl);

                String responseStr = XXX.callJsonHttp(url,para);
                System.out.println("Response of Home Open FD :: "+responseStr);

                return responseStr;

            }catch(Exception e)
            {
                return "Error";
            }
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
if(progressDialog!=null)
            progressDialog.dismiss();

        }
    }

当你退出 activity 然后在 Activity

的 onDestry() 方法中写下面的代码
 @Override
        protected void onDestroy() {
    if(NewConnections.progressDialog!=null)
                    NewConnections.progressDialog.dismiss();

            super.onDestroy();
        }

试试这个::希望这对你有帮助。

Dialog dialog;

    @Override

    protected void onPreExecute() {
        super.onPreExecute();
            showProgressBar();

    } 

@Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        try{
                hideProgressBar();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

public void showProgressBar() {
        dialog = new Dialog(mContext, android.R.style.Theme_Translucent);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        dialog.setCancelable(false);

        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View viewChild = inflater.inflate(R.layout.loader, null);

        dialog.setContentView(viewChild);

        Runtime.getRuntime().gc();
        System.gc();

        try {

            dialog.show();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

/**
     * Hide progress dialog
     */
    public void hideProgressBar() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

loader.xml::
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:gravity="center">

       <ProgressBar
        android:id="@+id/loading_spinner"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:indeterminateTintMode="src_atop"
        android:indeterminateTint="@color/colorAccent"
        android:layout_gravity="center" />


</RelativeLayout>