Download Code

Android Thread Example

In android thread example you will learn basic about threads,Exception and how to use it in android applications.First i will give some basic information.Link.

Thread(s)

A concurrent unit of execution.It has its own stack for methods to be invoked.Every application have at least one running when application started known as “Main and UI Threads“.Becuase it is responsible for every event occuring on UI.

There are two ways to execute your code.

  • Use subclass and Override run() method.
  • Create a new and pass Runnable to the constructor.

There are two basic rules for UI Threads.

  • Do not block.
  • Do not access the Android UI toolkit from outside.

Own integer priority that affect how this is scheduled by operating system.This priority is set using setPriority() method.

Exception

Exception is an error event that can happen during the execution of program and disturb program’s normal flow/execution.

There are some keywords we used while handling exceptions.Exception is basically used to handle irregular inputs.

  • Throw
  • Throws
  • Try-Catch
  • Finally

For more detail about exception handling you can go through this website.

Now go ahead and create android thread example application project.

thread,android thread,android thread example,thread example,

After creating new project.Your MainActivity.java by default will look like this asusual.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
   
}

We are going to follow first method.First create thread(s) variable and initialize.

public class MainActivity extends Activity {

	Thread basicThread;   // threads variable
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        basicThread = new Thread(){   	
        };
    }  
}

Now Override run() method.

public class MainActivity extends Activity {

	Thread basicThread;   // threads variable
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        basicThread = new Thread(){
        	// overriding run method
        	public void run(){
        	    // code goes here	
        	}
        };
    }
   
}

Till here you can put anything within the run method to execute.But i’ll use Exception handling to give you professional direction.

public class MainActivity extends Activity {

	Thread basicThread;   // threads variable
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        basicThread = new Thread(){
        	// overriding run method
        	public void run(){
        		// try block
        		try{
        			
        		}
        		// catch block
        		catch(InterruptedException e){
        			
        		}
        		// finally block it's optional
        		finally{
        			
        		}
        	}
        };
        //starting the threads
        basicThread.start();
    }
   
}

In this we will sleep this thread(s) for 5 second and start new activity using intent.So for that create new activity and layout.Most importantly don’t forget to declare activity in Android Manifest File.

MainActivity.java

/*
 * 
 * All Rights Reserved By: Farhan Rasheed
 * 
 * */

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends Activity {

	Thread basicThread;   // variable
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        basicThread = new Thread(){
        	// overriding run method
        	public void run(){
        		// try block
        		try{
        			sleep(5000);    // sleeping for 5 second
        		}
        		// catch block
        		catch(InterruptedException e){
        			e.printStackTrace();
        		}
        		// finally block it's optional
        		finally{
        			Intent n = new Intent(MainActivity.this,second.class);
        			startActivity(n);
        		}
        	}
        };
        //starting
        basicThread.start();
    }
   
}

Now Run your android application project.

thread,android thread,android thread example,thread example,

After Five second.

thread,android thread,android thread example,thread example,

This was a simple with low computations.But when user wants intensive work then this will perform poorly.

That’s it for this tutorial.

Thanks.