Saturday, August 6, 2011

welcome screen code using Thread and Handler Android Application



WelcomeThread.java
...............................


import android.content.Context;
public class WaitingThread extends Thread{
private    Context c;
private WaitingInterface mWaitingInterface;

public interface WaitingInterface
{
   public void onWaitingAppRerurn(final boolean result);
}
   
public WaitingThread(WaitingInterface ti) {
    mWaitingInterface=ti;
 }              

@Override
    public void run() {
        super.run();
        try {
            sleep(*1000);
            mWaitingInterface.onWaitingAppRerurn(true);
            
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }}}
--------------------------------------------------------------------------------------
WelcomeActivity.java
................................
public class WelcomeActivity extends Activity implements WaitingInterface{
    /** Called when the activity is first created. */
    private final int TEST_REQUEST = 1;
    protected Context mContext;
    protected MediaPlayer mPlayer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);


// How to Remove the title in android Application code        
   requestWindowFeature(Window.FEATURE_NO_TITLE);
// For full screen
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);




       setContentView(R.layout.welcome);

//Background Music for the welcome screen    
//     MediaPlayer example code for android application  


       MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.song1);
       mPlayer.start();

//     mPlayer.setLooping(true);


       mContext=this;
       new WaitingThread(WelcomeActivity.this).start();  

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
  //      mPlayer.stop();
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
    
     //How to Use Handler in Android 
    
     public Handler mHandler=new Handler(){
         @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
        switch (msg.what) {
            case TEST_REQUEST:
                Boolean isSuccess=(Boolean)msg.obj;
                if(isSuccess){
                     final Intent intent = new Intent(mContext, NextActivity1.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                    finish(); 
                } break;

            default:
            break;}}};
    public void onWaitingAppRerurn(boolean result)
 { mHandler.sendMessage(mHandler.obtainMessage(TEST_REQUEST,new Boolean(result))); }
}

1 comment:

  1. Nice Thread Tutorial, i am new in android , its help me a lot ...

    I have got some good links

    here
    at androidexample.com

    Thread_With_Handlers

    ReplyDelete