How does Android change AsyncTask to a non-internal class

When we use AsyncTask in Android, we generally use it as an inner class of Activity. The reason is that it is easy to directly operate the interface element controls of the Activity when updating the progress onProgressUpdate and onPostExecute when the execution is completed. But if we want our code to have a single responsibility and a clearer division of functions, it is best not to use inner classes. At this time, we can use Handler to achieve this requirement.

For the usage of AsyncTask, please refer to this blog I wrote: http://www.cnblogs.com/ghj1976/archive/2011/05/06/2039204.html

The above is to use AsyncTask as the inner class of Activity.

The following is an example where AsyncTask subclasses and AsyncTask subclasses are parallelized.

First the layout file: main.xml

Here are two controls, a progress bar and a textbox, both of which will be updated when the progress is updated:


   
   xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<ProgressBar android:id="@+id/progress_bar"
		android:layout_width="200dip" android:layout_height="200dip"
		android:layout_gravity="center" android:max="100" android:progress="0">
	
    
    ProgressBar>
	<TextView android:text="TextView" android:id="@+id/textView1"
		android:layout_width="wrap_content" android:layout_height="wrap_content">
     
     TextView>

      
      LinearLayout>

Subclass of AsyncTask: AsyncLoader, pay attention to the constructor here, which is one of the keys here.

package ghj1976.AsyncTask;

import android.os.AsyncTask;
import android.os.Handler;

// Set the three types of parameters as Params = Void, Progress = Integer, Result = Void 
public  class AsyncLoader extends AsyncTask
  
  
   
    {

	
   
   private Handler handle =
   
   null;

	
   
   public AsyncLoader(Handler h) {
		
   
   this.handler = h;
	}

	@Override
	
   
   protected Void doInBackground(Void... params) {
		publishProgress(10);
		
   
   try {
			Thread.sleep(2000);
		} 
   
   catch (InterruptedException e) {
			e.printStackTrace();
		}
		publishProgress(50);
		
   
   try {
			Thread.sleep(3000);
		}
   
   catch (InterruptedException e) {
			e.printStackTrace();
		}
		publishProgress(100);
		
   
   return 
   
   null;
	}

	@Override
	
   
   protected 
   
   void onPostExecute(Void v) {
		
   
   this.handler.sendEmptyMessage(0);
	}

	@Override
	
   
   protected 
   
   void onProgressUpdate(Integer... values) {
		
   
   this.handler.sendEmptyMessage(values[0]);
	}

}
  
  

AsyncTaskActivity code:

package ghj1976.AsyncTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class AsyncTaskActivity extends Activity {

	public ProgressBar pBar;

	private TextView tv;

	// The handler of the main thread 
	private Handler handler = new Handler() {
		 public  void handleMessage(Message msg) {

			tv.setText(msg.what + " ");
			if (msg.what <= 0) {
				pBar.setVisibility(View.INVISIBLE);
			} else {
				pBar.setProgress(msg.what);
			}
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		tv = (TextView) findViewById(R.id.textView1);
		tv.setText(" Ready to start ");

		pBar = (ProgressBar) findViewById(R.id.progress_bar);

		// AsyncTask.execute() needs to call 
		new AsyncLoader(handler).execute((Void) null );
	}

}

Guess you like

Origin blog.csdn.net/ghj1976/article/details/6425106