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 (UML term) 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...

8 comments:

Unknown said...

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

Unknown 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

Unknown said...

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

Jamess said...

To have more enhanced results and optimized benefits, you are able to take the help of experts making a call at QuickBooks Payroll Support Phone Number Well! If you’re not in a position to customize employee payroll in.

Jamess said...

To have more enhanced results and optimized benefits, you are able to take the help of experts making a call at QuickBooks Payroll Support Phone Number Well! If you’re not in a position to customize employee payroll in.

svrtechnologies said...

Thanks for sharing such an useful and informative stuff...

websphere training
mq tutorial

subha said...

your providing such a valuable information about studying..and also have some good key points to every student.keep update lot
Ai & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai