AsyncTask Class Example

There are two ways to offload main thread’s task
1) Java Thread Class
2) AsyncTask class

Following Code demonstrates how to use AsyncTask class to offload task of main thread.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="48dp"
        android:layout_toLeftOf="@+id/textView1"
        android:text="Button" />

</RelativeLayout>

AsyncTaskActivity.java

package com.example.asynctaskactivity;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnClickListener 
{
	Button btn;
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn = (Button) findViewById(R.id.button1);
	
		//because we implement OnClickListener we only have to pass "this" (much easier)
		btn.setOnClickListener(this);
	}

	public void onClick(View view)
	{

		switch(view.getId())
		{
			case R.id.button1:
//				for(int i=0;i&lt;5;i++) {
//					try {
//						Thread.sleep(1000);
//					} catch (InterruptedException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//				}
//				TextView txt = (TextView) findViewById(R.id.textView1);
//				txt.setText(&quot;Executed&quot;);
				new LongOperation().execute(&quot;&quot;);
				break;
		}	

	}
	private class LongOperation extends AsyncTask {

		@Override
		protected String doInBackground(String... params) {
			Log.d("xml", "DoinBackground");
			for(int i=0;i&lt;5;i++) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			return &quot;Executed&quot;;
		}      

		@Override
		protected void onPostExecute(String result) 
		{
			Log.d(&quot;xml&quot;, &quot;OnPostExecute&quot;);
			TextView txt = (TextView) findViewById(R.id.textView1);
			txt.setText(&quot;Executed&quot;); // txt.setText(result);
			//might want to change &quot;executed&quot; for the returned string passed into onPostExecute() but that is upto you
		}

		@Override
		protected void onPreExecute() 
		{
			Log.d(&quot;xml&quot;, &quot;OnPreExecute&quot;);
		}

		@Override
		protected void onProgressUpdate(Void... values) {
		}
	}
}
Advertisements