不确定进度条情况 [英] Indeterminate Progress-Bar Situation

查看:59
本文介绍了不确定进度条情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了 progressbar 作为不确定状态,并放入扩展了 asynctask 的类中.
doInBackground 方法中,我添加了一个带有 volley-library 的方法(例如 m_one ).在 m_one()中,我调用了方法 m_two(); .在 m_two()中,我叫 m_three(); .
现在的问题是, progressbar 仅在执行 m_one()之后, progressbar 可见性变为不可见,并且其余方法在后台线程中执行之前才起作用.br>在所有方法都使用截击请求完成数据接收之前,如何保持旋转状态 progressbar ?

I added a progressbar as indeterminate state and put in a class extends asynctask.
In doInBackground method, i added one method (say m_one) with volley-library.
In m_one(), i have called method m_two();. In m_two(), i have called m_three();.
Now, the problem is, progressbar only works till m_one() executes and then progressbar visibility goes invisible and remaining methods executes in background thread.
How do i keep rotating progressbar until all the methods done receiving data using volley request?

推荐答案

尝试以下代码.

        private static final String TAG = "LoginActivity.this";

        Button button_login;

        Context context;
        ProgressBar progressBar;

    //some variables for accepting the volley response data

        DBUtils dbUtils;
        Calendar calendar_2;
        public SimpleDateFormat dateFormat;
        public SimpleDateFormat timeformat;
        Date date;
        TreeSet<Integer> idSet;


        @RequiresApi(api = Build.VERSION_CODES.O)
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            button_login = findViewById(R.id.btn_login);

            context = getApplicationContext();
            progressBar = findViewById(R.id.progress_id_1);

            calendar_2 = Calendar.getInstance();
            dbUtils = new DBUtils(this);
            //clear the db tables on start of app
            dbUtils.ClearTables();


            date = Calendar.getInstance().getTime();
            Log.e(TAG, "\n current time: " + date);
            dateFormat = new SimpleDateFormat("dd-MM-yyyy");
            idSet = new TreeSet<>();

            //Login button click event  ********************
            button_login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    checkNetwork();

                }
            });
        }

        private void checkNetwork() {

            progressBar.setVisibility(View.VISIBLE);

            ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = conMgr.getActiveNetworkInfo();


            try {
                if (netInfo != null) {
                    NetworkInfo.State state = conMgr.getActiveNetworkInfo().getState();
                    if (state == NetworkInfo.State.CONNECTED) {
                        Log.e(TAG, "nw info: " + netInfo.getExtraInfo());
                        Log.e(TAG, "Network Connected ");
                        method1();
                        Thread.sleep(3000);
                    } else {
                        Log.e(TAG, "Network Disconnected");
                    }
                } else if (netInfo == null) {
                    Log.e(TAG, "Network Not Found !");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    //          ********************************************
    //          ********************************************
    //to get the response of requested url

        public void method1() {

            try {
                //url without api for login session
                URL loginURL = new URL(your url 1);
                HttpURLConnection urlconnect = (HttpURLConnection) loginURL.openConnection();
                ;

                //http connection for webpage permission

                //type of request get / post...
                urlconnect.setRequestMethod("POST");

                //allow the post request for user
                urlconnect.setDoOutput(true);

     /*
     Singleton_Volley is the class for volley initialize

     you can skip Singleton_Volley class..its correct
     */
                // Get a RequestQueue in required page
                RequestQueue queue = Singleton_Volley.getInstance(this.getApplicationContext()).
                        getRequestQueue();

    // Request a string response from the provided URL.(my case, url)

                StringRequest stringRequest = new StringRequest(Request.Method.POST, "" + loginURL.toString(),
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {

                                //accept the data from request and set in our views like list,text, etc...


                                try {
                                    /* your response data stored in variables first and then stored in sqlite you have to consider this strictly, make sure you also have array in response to wait for a while */

                                    method2();

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
    //popup an error message...(Toast)

                    }
                })
            } ;
            // Add the request to the RequestQueue.
            Singleton_Volley.getInstance(this).addToRequestQueue(queue.add(stringRequest));
        } catch(
        IOException e)

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

        {
            e.printStackTrace();
        }
    }

        public void method2() {
            try {

                url_2 = new URL(your url 2);
                HttpURLConnection urlconnect = (HttpURLConnection)
                        url_2.openConnection();
                ;

                //http connection for webpage permission

                //type of request get / post...
                urlconnect.setRequestMethod("POST");

                //allow the post request for user
                urlconnect.setDoOutput(true);

                RequestQueue contactQueue = Singleton_Volley.getInstance(this).
                        getRequestQueue();

    // Request a string response from the provided URL.(in my case, url_2)
                StringRequest stringRequest = new StringRequest(Request.Method.GET, "" + url_2.toString(),
                        new Response.Listener<String>() {
                            @RequiresApi(api = Build.VERSION_CODES.O)
                            @Override
                            public void onResponse(String response) {

    //accept the data from request and set in our views like list,text, etc...
                                try {

                               /*
                                your response data stored in variables first and then stored in sqlite

                               you have to consider this strictly
                               make sure you also have array in response to wait for a while

                               */
                                    method3(); //method3() called here
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                        //popup an error messege...(Toast)
                        Toast.makeText(LoginActivity.this, "" + error, Toast.LENGTH_SHORT).show();
                    }
                })
            } ;
            // Add the request to the RequestQueue.
            Singleton_Volley.getInstance(this).addToRequestQueue(contactQueue.add(stringRequest));

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

    //          ********************************************
    //          ********************************************
     private void method3(){

            try{
            URL url_3=new URL(your url 3);
            HttpURLConnection urlconnect=(HttpURLConnection)url_3.openConnection();
            ;

            //http connection for webpage permission
            //type of request get / post...
            urlconnect.setRequestMethod("POST");

            //allow the post request for user
            urlconnect.setDoOutput(true);


            RequestQueue stageQueue=Singleton_Volley.getInstance(this).
            getRequestQueue();

            // Request a string response from the provided URL.(in my case, url_3)
            StringRequest stringRequest=new StringRequest(Request.Method.GET,""+url_3.toString(),
            new Response.Listener<String>(){
     @RequiresApi(api = Build.VERSION_CODES.O)
     @Override
     public void onResponse(String response){

            //accept the data from request and set in our views like list,text, etc...
            try{
            progressBar.setVisibility(View.GONE);
                                /*
                                make sure you also have array in response to wait for a while
                                */
            }catch(Exception ex){
            ex.printStackTrace();
            }

            }
            },new Response.ErrorListener(){
     @Override
     public void onErrorResponse(VolleyError error){

            //popup an error messege...(Toast)

            }
            })
            };
            // Add the request to the RequestQueue.
            Singleton_Volley.getInstance(this).addToRequestQueue(stageQueue.add(stringRequest));

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

            startActivity(new Intent(LoginActivity.this,Page2.class));
            }

            }
            }}

请管理结束符},它应该可以工作.

Please manage the closing } and it should work.

这篇关于不确定进度条情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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