Handler - usando um Runnable para splash

Top  Previous  Next

Tela

 

IsplashActivity.java

 
package com.splashi;

 

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.os.Handler;

import android.widget.Toast;

 

public class IsplashiActivity extends Activity implements Runnable {

 

 private final int DELAY = 3000;

 

 @Override

 public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         // exibe a imagem

         setContentView(R.layout.layout_splash);

         Toast.makeText(this, "aguarde o carregamento", Toast.LENGTH_SHORT).show();

         // solicita para o handler excutar o Runnable (this) fechando a slash

         Handler h = new Handler();

         h.postDelayed(this, DELAY);

 }

 

 public void run() {

         // abre a principal

         startActivity(new Intent(this, Principal.class));

         // finish activity

         finish();

 }

}

 

layout_splash.xml

 
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:orientation="vertical" >

 

  <ImageView

      android:id="@+id/imageView1"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:src="@drawable/tiger" />   (figura do tigre)

 

</LinearLayout>

 

Principal.java

 
package com.splashi;

 

import android.app.Activity;

import android.os.Bundle;

 

public class Principal extends Activity {

 @Override

 public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         setContentView(R.layout.main);

 }

}

 

main.xml

 
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:orientation="vertical" >

 

  <TextView

      android:layout_width="fill_parent"

      android:layout_height="wrap_content"

      android:text="@string/hello" />

 

</LinearLayout>

 

Manifest

 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

  package="com.splashi"

  android:versionCode="1"

  android:versionName="1.0" >

 

  <uses-sdk android:minSdkVersion="7" />

 

  <application

      android:icon="@drawable/ic_launcher"

      android:label="@string/app_name" >

      <activity

          android:name=".IsplashiActivity"

          android:label="@string/app_name" >

          <intent-filter>

              <action android:name="android.intent.action.MAIN" />

              <category android:name="android.intent.category.LAUNCHER" />

          </intent-filter>

      </activity>

      <activity android:name=".Principal" />

  </application>

 

</manifest>