Monday, November 21, 2011

An implementation of Finite State Machine in Android using Handler...

Note: If you want to know about a full-fledged application about how a long running task in a background service has been broken into small states please have a look at this article.

As i was doing some research on different concepts in Android, i came out with this implementation of Finite State Machine using the Handler class... i would like to share it with you... the state diagram of the application will look as the following:




The main Activity class looks like the following:

package com.android.training.statepattern;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;



public class StatePatternActivity extends Activity implements View.OnClickListener{
 private Button startStatePattern;
 private MyHandler handler;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        startStatePattern = (Button)findViewById(R.id.button1);
        
        startStatePattern.setOnClickListener(this);
        handler = new MyHandler(this);
    }
    
    public void onClick(View v){
     if(v.equals(startStatePattern)){
      handler.sendEmptyMessage(1);
      }
    }  
}

And the other class is as follows;
package com.android.training.statepattern;

import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

public class MyHandler extends Handler {
 private StatePatternActivity MainActivity;
 
 MyHandler(StatePatternActivity a){
  this.MainActivity = a;
 }
 //state transition logic is strongly coupled with the different states...
  public void handleMessage(Message msg){
   switch(msg.what){
   case 1:
    ////do the processing
    Toast.makeText(MainActivity.getApplicationContext(),"Inside State 1", 2000).show();
    this.sendEmptyMessage(2);
    break; 
   case 2:
    ///do the processing
    Toast.makeText(MainActivity.getApplicationContext(),"Inside State 2", 2000).show();
    this.sendEmptyMessage(3);
    break;    case 3:
    ///do the processing
    Toast.makeText(MainActivity.getApplicationContext(),"Inside State 3", 2000).show();
    this.sendEmptyMessage(4);
    break;
   case 4:
    break;
  default:
   break;   
   }
  } 
}

Let me explain the code a bit... here the crux of the solution lies in the MyHandler class derived from Handler.. The main activity class HAS MyHandler..
We start the State machine by sending a message (integer 1) through the function SendEmptyMessage called on MyHandler object. As a result, the callback function called handleMessage of the Handler is called and we handle this state's functionality here... the next all starte transitions happen sequentially from this callback function...
Hope this throws some lights on a specific design concept in Android...

4 comments:

sushene wilson said...

Thanks for providing such a use full code. <a href="http://www.systemarchitects.ca/>IT Solutions</a>

sushene wilson said...

Thanks for providing such a use full code. <a href="http://www.systemarchitects.ca/>IT Solutions</a>

srikanth said...

your providing such a valuable information about studying..and also have some good key points to every student.

IBM MQ online training

kits online said...

very nice article.Thanks for sharing the post...!
PHP Online Training

Share