AsyncTask and Contexts

后端 未结 5 1417
情书的邮戳
情书的邮戳 2020-12-28 16:59

So I\'m working out my first multi-threaded application using Android with the AsyncTask class. I\'m trying to use it to fire off a Geocoder in a second thread, then update

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-28 17:27

    @Eugene van der Merwe

    The following piece of code works for me : ) -->

    public class ApplicationLauncher extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.applicationlauncher);
    
        LoadApplication loadApplication = new LoadApplication(this);
        loadApplication.execute(null);
    }
    
    private class LoadApplication extends AsyncTask {
    
        Context context;
        ProgressDialog waitSpinner;
        ConfigurationContainer configuration = ConfigurationContainer.getInstance();
    
        public LoadApplication(Context context) {
            this.context = context;
            waitSpinner = new ProgressDialog(this.context);
        }
    
        @Override
        protected Object doInBackground(Object... args) {
            publishProgress(null);
            //Parsing some stuff - not relevant
            configuration.initialize(context);
            return null;
        }
    
        @Override
        protected void onProgressUpdate(Object... values) {
            super.onProgressUpdate(values);
            // Only purpose of this method is to show our wait spinner, we dont
            // (and can't) show detailed progress updates
            waitSpinner = ProgressDialog.show(context, "Please Wait ...", "Initializing the application ...", true);
        }
    
        @Override
        protected void onPostExecute(Object result) {
            super.onPostExecute(result);
            waitSpinner.cancel();
        }
    }
    }
    

    Cheers,

    Ready4Android

提交回复
热议问题