简体   繁体   中英

Android: How do I create loading screen while access the database

I have been developing one Map project. In all Activity classes i have one method called getDataFromServer() which parse some JSON Response from the server for the particular activity. And displays that data on the Map. While receiving and parse data , I want to display loading screen. How to make getDataFromServer() method in Doinbackground().

Please provide me the best way........ Thanks..

Use AsyncTask :

public final class DownloadFile extends AsyncTask<Void, Long, Boolean> {

private Context context;
private ProgressDialog progressDialog;

public DownloadFile (Context context) {
    this.context = context;
}

/* 
 * @see android.os.AsyncTask#onPreExecute()
 */
@Override
protected void onPreExecute() {
    try {
        progressDialog = ProgressDialog.show(context, "", "message", true);
    } catch (final Throwable th) {
        //TODO
    }
}

/* 
 * @see android.os.AsyncTask#doInBackground(Params[])
 */
@Override
protected Boolean doInBackground(Void... arg0) {
    //do something
}

    @Override
protected void onProgressUpdate(String... progress) {
    //do something
    super.onProgressUpdate(progress);
}

/* 
 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
 */
@Override
protected void onPostExecute(Boolean result) {
    progressDialog.dismiss();
} }

Refer this link

http://www.bango29.com/go/blog/2011/android-asynctask-is-a-beauty-part-2

In button on clicklistener, you can create object.

new Classname().execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM