在机器人为什么我的进度条冻结?我的、机器人、进度条

2023-09-06 13:15:39 作者:当地比较有名的小可爱

我显示进度条在检索来自服务器的数据的时间和从服务器获取数据后显示在图表上,但在绘制的图表,数据我的进度条获得冻结时间的数据没有任何一个有想法,为什么会这样。 ......

在此先感谢...

 私人ProgressDialog PD;
私人处理程序处理程序=新的处理程序(){
    @覆盖
    公共无效的handleMessage(信息MSG){
        Toast.makeText(背景下,请等待......,Toast.LENGTH_LONG).show();
        线程t =新主题(新的Runnable(){
            @覆盖
            公共无效的run(){
                functionDrawMyData(); / *在这个函数中,我访问活动的看法,并在画的时候对这种观点绘制数据我的进度条冻结* /
            }
        });
        runOnUiThread(T);
        pd.dismiss();
    }
};
 

此处理我使用的是检索完数据之后调用 和按钮单击我得到的数据并显示进度条

 的ImageButton myButton的=(的ImageButton)findViewById(R.id.myBtn);
pair1ChartButton.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
                PD = ProgressDialog.show(v.getContext(),请稍候...,检索数据...,真实,
                        真正,
                        新DialogInterface.OnCancelListener(){
                            @覆盖
                            公共无效OnCancel的(DialogInterface对话){

                            }
                        });
                线程t =新主题(新的Runnable(){
                    @覆盖
                    公共无效的run(){
                            getDataFromServer(); //调用函数来获得来自服务器的数据
                        handler.sendEmptyMessage(0);
                    }
                });
                t.start();
        }
    });
 

解决方案 cs1.6每次一加机器人就出这个进度条而且卡的要死,好像是加了某个地图后就这样了,是什么文件的原因

您应该这样做具有的 AsyncTask的(智能底色线程)和的 ProgressDialog

的AsyncTask能够适当且易于使用的用户界面线程。该类允许无需操作线程和/或处理程序进行后台操作并发布在UI线程上的结果。

这是异步任务是由在后台线程运行的计算,其结果发表在UI线程上定义。异步任务由3泛型类型定义,所谓的PARAMS,进展和成效,以及4个步骤,称为开始,doInBackground,processProgress和结束。

4步骤

当执行一个异步任务,任务经过4个步骤:

在preExecute(),执行任务后,立即在UI线程调用。这个步骤是通过示出在用户界面的进度条通常用于设置任务,例如

doInBackground(参数...),在后台线程上preExecute后立即调用()完成执行。这个步骤是用于执行后台计算,可以采取很长的时间。异步任务的参数被传递到这一步。计算的结果,必须通过此步骤被返回,将被传递回的最后一步。这一步也可以使用publishProgress(进展...)公布进展情况的一个或多个单元。这些值公布在UI线程上,在onProgressUpdate(进展...)一步。

onProgressUpdate(进展...),调用publishProgress(进展......)之后的UI线程调用。的执行的计时是不确定的。此方法用于显示的任何形式的用户界面中的进展而背景计算仍在执行。例如,它可以用于动画在文本字段进度条或显示日志

onPostExecute(结果),后台计算完成后在UI线程调用。背景计算的结果被传递给此步骤作为一个参数。 线程规则

有必须遵循此类正常工作的几个线程的规则:

任务实例必须在UI线程上创建。 执行(参数...)必须在UI线程中调用。 不要在preExecute调用(),onPostExecute(结果),doInBackground(参数...),onProgressUpdate(进步...)手动。 该任务只能一次(如果是第二次执行尝试一个异常将被抛出。)

执行

示例code 什么适配器确实在这个例子中并不重要,更重要的是了解你需要使用的AsyncTask来显示该进程的对话框。

 私有类prepareAdapter1延伸的AsyncTask<虚空,虚空,ContactsListCursorAdapter> {
    ProgressDialog对话框;
    @覆盖
    在preExecute保护无效(){
        对话框=新ProgressDialog(viewContacts.this);
        dialog.setMessage(的getString(R.string.please_wait_while_loading));
        dialog.setIndeterminate(真正的);
        dialog.setCancelable(假);
        dialog.show();
    }
    / *(非Javadoc中)
     * @see android.os.AsyncTask#doInBackground(PARAMS [])
     * /
    @覆盖
    保护ContactsListCursorAdapter doInBackground(空... PARAMS){
        CUR1 = objItem.getContacts();
        startManagingCursor(CUR1);

        适配器1 =新ContactsListCursorAdapter(viewContacts.this,
                R.layout.contact_for_listitem,CUR1,新的String [] {},新的INT [] {});

        返回适配器1;
    }

    保护无效onPostExecute(ContactsListCursorAdapter结果){
        list.setAdapter(结果);
        dialog.dismiss();
    }
}
 

i am showing Progress Bar at time of retrieving data from server and after retrieving data from server showing that data on chart but at time of plotting that data on chart my progress bar get freezes does any one have idea why this is so......

THANKS in advance...

private ProgressDialog pd;
private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {        
        Toast.makeText(context, "Please Wait...", Toast.LENGTH_LONG).show();
        Thread t = new Thread(new Runnable() {              
            @Override
            public void run() {
                functionDrawMyData();/*in this function i am accessing activity view and drawing data on that view at time of drawing my Progress bar Freezes */
            }
        });
        runOnUiThread(t);
        pd.dismiss();           
    }
};  

this handler i am using which is called after retrieving data finished and on button click i am getting data and showing progress bar

ImageButton myButton = (ImageButton) findViewById(R.id.myBtn);
pair1ChartButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {               
                pd = ProgressDialog.show(v.getContext(),"Please wait...","Retrieving data ...",true,
                        true,
                        new DialogInterface.OnCancelListener(){
                            @Override
                            public void onCancel(DialogInterface dialog) {

                            }
                        });
                Thread t = new Thread(new Runnable() {                      
                    @Override
                    public void run() {
                            getDataFromServer();//calling function to get data from server
                        handler.sendEmptyMessage(0);                    
                    }
                });
                t.start();
        }
    });

解决方案

You should do this with the help of AsyncTask (an intelligent backround thread) and ProgressDialog

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called begin, doInBackground, processProgress and end.

The 4 steps

When an asynchronous task is executed, the task goes through 4 steps:

onPreExecute(), invoked on the UI thread immediately after the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface.

doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.

onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter. Threading rules

There are a few threading rules that must be followed for this class to work properly:

The task instance must be created on the UI thread. execute(Params...) must be invoked on the UI thread. Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually. The task can be executed only once (an exception will be thrown if a second execution is attempted.)

Example code What the adapter does in this example is not important, more important to understand that you need to use AsyncTask to display a dialog for the progress.

private class PrepareAdapter1 extends AsyncTask<Void,Void,ContactsListCursorAdapter > {
    ProgressDialog dialog;
    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(viewContacts.this);
        dialog.setMessage(getString(R.string.please_wait_while_loading));
        dialog.setIndeterminate(true);
        dialog.setCancelable(false);
        dialog.show();
    }
    /* (non-Javadoc)
     * @see android.os.AsyncTask#doInBackground(Params[])
     */
    @Override
    protected ContactsListCursorAdapter doInBackground(Void... params) {
        cur1 = objItem.getContacts();
        startManagingCursor(cur1);

        adapter1 = new ContactsListCursorAdapter (viewContacts.this,
                R.layout.contact_for_listitem, cur1, new String[] {}, new int[] {});

        return adapter1;
    }

    protected void onPostExecute(ContactsListCursorAdapter result) {
        list.setAdapter(result);
        dialog.dismiss();
    }
}