AsyncTask/threading - Help

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

AsyncTask/threading - Help

Postby Untouchab1e » Sat Jun 19, 2010 11:44 am

So, I have an app that writes and POSTs a .xml file to the web. However, my app obviously freezes up when its working so I realise I need to put all this in a dedicated thread or something. I have been reading up on the subject a bit, but I am still struggling to make it work. All I want to do is show a loading ProgressDialog (not the one with the loading bar, but just a spinning circle).

Can anyone give me some help? My code is pretty simple. When the user press the "upload" button, the app just runs the "do_update ();" routine.

Thanks
Untouchab1e
Freshman
Freshman
 
Posts: 8
Joined: Tue Jun 02, 2009 8:24 am

Top

Re: AsyncTask/threading - Help

Postby padde » Sat Jun 19, 2010 2:12 pm

This should do the trick...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Test extends Activity {
  2.     public void onCreate(Bundle savedInstanceState) {
  3.         super.onCreate(savedInstanceState);
  4.         setContentView(R.layout.main);
  5.                  
  6.         Button upload = (Button) findViewById(R.id.update_Button);
  7.        
  8.         upload.setOnClickListener(new OnClickListener() {
  9.             public void onClick(View pV) {
  10.                 new DoInBackground().execute();
  11.             }
  12.         });
  13.     }
  14.    
  15.     public void do_update() {
  16.         // your stuff here
  17.     }
  18.  
  19.     private class DoInBackground extends AsyncTask<Void, Void, Void>
  20.                                  implements DialogInterface.OnCancelListener
  21.     {
  22.         private ProgressDialog dialog;
  23.        
  24.         protected void onPreExecute() {
  25.             dialog = ProgressDialog.show(Test.this, "", "Busy...", true, true, this);
  26.         }
  27.        
  28.         protected Void doInBackground(Void... v) { do_update(); return null; }
  29.         protected void onPostExecute(Void v) { dialog.dismiss(); }
  30.         public void onCancel(DialogInterface dialog) { cancel(true); dialog.dismiss(); }
  31.     }
  32. }
  33.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Re: AsyncTask/threading - Help

Postby Untouchab1e » Sat Jun 19, 2010 3:07 pm

Thanks mate, much appreciated!

I now call new DoInBackground().execute(); and the progressDialog shows, however, seemingly when its supposed to dismiss, I get a force close!

Any ideas?

EDIT: Pretty sure it has something to do with the do_update() method. I tried changing it to a couple of others, most of them also resulted in a force close but a few did work. (Methods that hardly do anything). The do_update method downloads a echo output from a php script and sets it as text in a text view..

EDIT: Commented out the two lines where the method sets the text of the text view and such and then it worked.

EDIT: I placed the text in a variable and updated the text view text in protected void onPostExecute(Void v) instead. Worked like charm! Thank you so much!
Untouchab1e
Freshman
Freshman
 
Posts: 8
Joined: Tue Jun 02, 2009 8:24 am

Re: AsyncTask/threading - Help

Postby limteckchai » Thu Jul 22, 2010 4:32 am

Hey, Thanks for the codes but can anyone tell me how to modify it for the use of in this context , i realise the other is made for a button or something else??

Code: Select all


public class ASYNC extends ListActivity {
   public String hello = "";
   public boolean source = false;
   final Context ab = this;

   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      Bundle extras = getIntent().getExtras();
      hello = extras.getString("selection");
      source = extras.getBoolean("Searchbox");

      DoInBackground.execute();
   

   }






   private class DoInBackground extends AsyncTask<Void, Void, Void> {

      private ProgressDialog dialog;

      protected void onPreExecute() {

         dialog = ProgressDialog.show(makan_list_results.this, "",
               "Busy...", true, true, (OnCancelListener) this);

      }

      protected Void doInBackground(Void... v) {
         do_update();
         return null;
      }

      protected void onPostExecute(Void v) {
         dialog.dismiss();
      }


   }

   public void do_update() {
      
      
         try {
            
            //my methods here
            //   GETTING XML N SETTING XML DATA IN TO LISTactivity

         } catch (Exception e) {

            // error msg

         }
      }
      

}


Currently the problem i face is that when i cannot compile as there is an error on the line ( DoInBackground.execute(); )

Cannot make a static reference to the non-static method execute(Void...) from the type AsyncTask<Void,Void,Void>

My other codes are working fine before the implementation of AsyncList.

Please advise
Teck Chai

Oh btw, Eclipse user here, 2.2 API level. Yogurt FTW
limteckchai
Experienced Developer
Experienced Developer
 
Posts: 92
Joined: Mon Jun 28, 2010 3:33 am

Re: AsyncTask/threading - Help

Postby Maddes » Wed Aug 25, 2010 12:42 pm

Maybe this will help you, it's a pretty good german Tutorial on threading:
http://androidprojects.wordpress.com/20 ... roid-apps/
If you don't speak german, just post here again and I'll do my best to share my knowlegde I earned through that tutorial!
Maddes
Freshman
Freshman
 
Posts: 5
Joined: Sat Aug 07, 2010 1:28 pm

Re: AsyncTask/threading - Help

Postby Brocade » Mon Aug 30, 2010 7:32 pm

you shouldnt (can not?) reference the dialog outside of the view, you need to create a message handler and show/dismiss the dialog from within the view (handler). send a message to the handler to update the progress dialog that way.
Brocade
Freshman
Freshman
 
Posts: 4
Joined: Sat Jul 03, 2010 2:42 pm

Top

Re: AsyncTask/threading - Help

Postby ArtworkAD » Tue Nov 23, 2010 2:50 pm

hi all, androidprojects.wordpress.com moved to http://android-project.de. visit http://android-projects.de/2010/08/13/threading-in-android-apps/ for the multithreading tutorial
ArtworkAD
Junior Developer
Junior Developer
 
Posts: 19
Joined: Thu Jul 29, 2010 7:56 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 12 guests