开发者

Thread implementation

when i press my login button there is a webservice call , at the time of web service call i have to display a progress dialog, and after receiving webservice return an intent to next page... but it always diplays a blank page when intent is called also progress dialog is not showing

    if(Utility.isNetworkAvailable(currActivity)){                 
//  Utility.showProgressDialog(currActivity);   
                            new Thread() {
                                @Override
                                public void run() {                             
                                    currActivity.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() { 
                                            Intent intent = new Intent(Login.this, Listviewer.class);
                                            startActivity(intent);
                                            tempSplashTread = new Thread() { // Thread for showing wait cursor
                                                @Override
                                                public void run() {
                                                    currActivity.runOnUiThread(new Runnable() {
                                                        @Override
                                                        public void run() { 
                                                            myProgressDialog = new ProgressDialog(InfraSignPdfList.this);       
                                                            myProgressDialog.setMessage("Loading...                  ");
                                                          开发者_开发技巧  myProgressDialog.show();
                                                        }
                                                    });
                                                }
                                            };
                                            tempSplashTread.start();
                                            finish();
                                        }
                                    });
                                }
                            }.start();
                        }else {
                            Utility.noNetworkDialog(currActivity);                          
                        }


It's a bad idea to put a progress dialog between launching apps. You need to implement the progress dialog in the one that does the download and then display the next Activity after the downloaded data has been passed to the new activity.

Follow below steps:

1.After Login Button start server call to get data from server till then display progressDialog on current activity.

2.After the data has been downloaded start the next Activity by passing the necessary data to it.


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();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜