Thursday 31 May 2012

SplashScreen

package com.ann;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.MotionEvent;
import android.widget.TextView;

public class splashSereen extends Activity {
    protected boolean _active = true;
    protected int _splashTime = 10000;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
      
        SharedPreferences app_preferences =
                PreferenceManager.getDefaultSharedPreferences(this);
          
            // Get the value for the run counter
            int counter = app_preferences.getInt("counter", 0);
            if(counter==0)
            {
                 TextView txt=(TextView)findViewById(R.id.textView1);
               //  txt.setText(read());
               
                 // thread for displaying the SplashScreen
                 Thread splashTread = new Thread() {
                     @Override
                     public void run() {
                         try {
                             int waited = 0;
                             while(_active && (waited < _splashTime)) {
                                 sleep(100);
                                 if(_active) {
                                     waited += 100;
                                 }
                             }
                         } catch(InterruptedException e) {
                             // do nothing
                         } finally {
                             finish();
                             startActivity(new Intent("com.ann.MyBirthdayReminderActivity"));
                             stop();
                         }
                     }
                 };
                 splashTread.start();
            }
            else
            {
                Intent intent=new Intent(this,MyBirthdayReminderActivity.class);
                startActivity(intent);
            }
            SharedPreferences.Editor editor = app_preferences.edit();
            editor.putInt("counter", ++counter);
            editor.commit(); // Very important
    }
  
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            _active = false;
        }
        return true;
    }
  private String read()
  {
InputStream inputStream = getResources().openRawResource(R.raw.birth);
     
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      
        int i;
     try {
      i = inputStream.read();
      while (i != -1)
         {
          byteArrayOutputStream.write(i);
          i = inputStream.read();
         }
         inputStream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
  
        return byteArrayOutputStream.toString();
   }
}


Splash screen using Activity


package com.ann;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;

public class splash extends Activity {
    protected boolean _active = true;
    protected int _splashTime = 5000;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
       
        // thread for displaying the SplashScreen
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(_active && (waited < _splashTime)) {
                        sleep(100);
                        if(_active) {
                            waited += 100;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    finish();
                    startActivity(new Intent("com.ann.start"));
                    stop();
                }
            }
        };
        splashTread.start();
    }
   
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            _active = false;
        }
        return true;
    }
}

No comments:

Post a Comment