Android splash screen Splash

Method 1, two Activities

Core code:

package ghj1976.HelloWorld;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashActivity extends Activity {

	private  final  int SPLASH_DISPLAY_LENGHT = 8000; // delay eight seconds

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);

		new Handler().postDelayed(new Runnable() {
			public void run() {
				Intent mainIntent = new Intent(SplashActivity.this,
						HelloWorldActivity.class);
				SplashActivity.this.startActivity(mainIntent);
				SplashActivity.this.finish();
			}

		}, SPLASH_DISPLAY_LENGHT);

	}
}

illustrate:

Handler().postDelayed is to delay the execution of the specified time

The Handler class can mainly use the following three methods to set the execution time of the Runnable object:

// Execute the Runnable object immediately   
public  final  boolean post(Runnable r);  
 // Execute the Runnable object at the specified time (uptimeMillis)   
public  final  boolean postAtTime(Runnable r, long uptimeMillis);  
 // Execute at the specified time interval (delayMillis) Runnable object   
public  final  boolean postDelayed(Runnable r, long delayMillis);

For more details about the Handler class, you can read this article: http://book.51cto.com/art/201006/207064.htm

The following two lines of code start a new Activity and close the current Activity at the same time.

SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();

对 finish 方法的解释如下: http://android.toolib.net/reference/android/app/Activity.html
Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().

image

Picture from: http://www.ibm.com/developerworks/cn/opensource/os-cn-android-actvt/

As shown above, Android programmers can decide the "life" of an Activity, but not its "death", that is to say, the programmer can start an Activity, but cannot manually "end" an Activity.

When you call the Activity.finish() method, the result is the same as when the user presses the BACK key: it tells the Activity Manager that the Activity instance has completed its work and can be "recycled".

Then the Activity Manager activates the Activity in the second layer of the stack and re-stacks it. At the same time, the original Activity is pushed into the second layer of the stack, changing from the Active state to the Paused state.

For example, in the above example: if HelloWorldActivity is started from SplashActivity, then HelloWorldActivity is currently at the top of the stack, and the second layer is SplashActivity.

When we call the SplashActivity.finish() method (we call it through SplashActivity.this.finish() in SplashActivity), SplashActivity transitions from the Active state to the Stopped state and is removed from the stack by the system, and the flag can be "recycled ".

The relationship between the state of the Activity and its position in the stack is as follows:

image

 

The example above is

Activity2 is started from Activity1, then Activity2 is currently at the top of the stack, and the second layer is Activity1. When we call the Activity2.finish() method in Activity2, Activity Manager reactivates Activity1 and puts it on the stack, and Activity2 transitions from the Active state Stopped state, and mark that Activity2 can be "recycled". Activity1.onActivityResult(int requestCode, int resultCode, Intent data) method is executed, and the data returned by Activity2 is returned to Activity1 through the data parameter.

 

Method 2: An Activity

 

layout file:

  
  1.0" encoding="utf-8"?>

  
  
   
   http://schemas.android.com/apk/res/android"
	android:orientation="
   
   vertical" android:layout_width="
   
   fill_parent"
	android:layout_height="
   
   fill_parent">
	
   
   
    
    @+id/splashscreen"
		android:orientation="
    
    vertical" android:layout_width="
    
    fill_parent"
		android:layout_height="
    
    fill_parent">

		
    
    
     
     @+id/info" android:layout_width="
     
     fill_parent"
			android:layout_height="
     
     wrap_content" android:gravity="
     
     center"
			android:paddingTop="
     
     10px" android:text="
     
     This is a splash !" />
	
    
    
   
   

	
   
   
    
    fill_parent"
		android:paddingTop="
    
    10px" android:layout_height="
    
    wrap_content"
		android:text="
    
    This is a Context" />

   
   
  
  

illustrate:

Here is a LinearLayout with an id of splashscreen, which is the part that appears when the program starts. It will be hidden when startup is complete.

Core code:

package ghj1976.AndroidTest;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	private LinearLayout splash;
	private TextView tv;

	private static final int STOPSPLASH = 0;
	// time in milliseconds
	private static final long SPLASHTIME = 1000;

	private Handler splashHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case STOPSPLASH:
				SystemClock.sleep(4000);	
				splash.setVisibility(View.GONE);
					break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_PROGRESS);
		setContentView(R.layout.main);

		splash = (LinearLayout) findViewById(R.id.splashscreen);
		tv = (TextView) findViewById(R.id.info);
		tv.setText(" Data connection is being established ");
	
		Message msg = new Message();
		msg.what = STOPSPLASH;
		splashHandler.sendMessageDelayed(msg, SPLASHTIME);
	}
}

illustrate

We send a message after the application starts to set the specified area as hidden, and splash.setVisibility(View.GONE); realizes the startup interface.

Guess you like

Origin blog.csdn.net/ghj1976/article/details/6394132