APP启动界面SplashActivity的两种实现方法

第一种:用两个activity实现

用Handler对象飞postDelayed方法来实现延迟跳转的目的;
补充:
1 // 立即执行Runnable对象
public final boolean post(Runnable r);

2 //在指定的时间uptimeMillis 执行Runnable对象

public final boolean postAtTime(Runnable r, long uptimeMillis);

3 // 在指定的时间间隔 delayMillis 执行Runnable对象

public final boolean postDelayed(Runnable r, long delayMillis);

1, activity_splash.xml

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


2,activity_main.xml
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.lyt.demo04_wakeuptimer.MainActivity" >


3, SplashActivity.java

package com.lyt.demo04_wakeuptimer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.Window;

public class SplashActivity extends Activity {

private final int SPLASH_DISPLAY_LENGHT=3000;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    //隐藏标题栏
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    
    //加载布局文件
    setContentView(R.layout.activity_splash);
    
    handler=new Handler();
    
    //延迟 SPLASH_DISPLAY_LENGHT时间然后跳转到MainActivity
    handler.postDelayed(new Runnable() {
        
        @Override
        public void run() {
            Intent intent =new Intent();
            intent.setClass(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            SplashActivity.this.finish();
            
        }
    }, SPLASH_DISPLAY_LENGHT);  
}

/* 禁用返回键
 *(non-Javadoc)
 * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode==KeyEvent.KEYCODE_BACK){
        
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

}

4, MainActivity.java
package com.lyt.demo04_wakeuptimer;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
/**

  • @author Administrator
  • 定时唤醒系统
  • 1,定时唤醒系统
  • 2,记录3G状态
  • 3,记录GPS状态
  • 4,

*/
public class MainActivity extends Activity {

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

}

5,修改AndroidManifest.xml文件:
    ...
2      3 android:name=".SplashActivity"
4 android:label="splash" >
5
6
7
8
9

10
11 12 android:name=".MainActivity"
13 android:label="@string/app_name" >
14
15      ...

APP启动界面SplashActivity的两种实现方法_第1张图片
timg (2).jpg

第二种:方法 用一个activity实现Splash启动界面
用一个Activity实现**
  主要利用控件的隐藏来实现。
  1、xml文件:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

         

               
           

         

2、MainActivity
package com.example.splashtest2;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
private final int STOP_SPLASH = 0;
private final int SPLASH_TIME = 3000;
private LinearLayout splashLt;

          private Handler splashHandler = new Handler() {
                  public void handleMessage(Message msg) {
                  switch (msg.what) {
                           case STOP_SPLASH:
                           splashLt.setVisibility(View.GONE);
                            break;
                            default:
                             break;
                      }
            super.handleMessage(msg);
              }
          };

@Override
  protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
            getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_main);

             splashLt = (LinearLayout) findViewById(R.id.splash_lt);

              Message msg = new Message();
              msg.what = STOP_SPLASH;

      // 注:这里必须用延迟发送消息的方法,否则ImageView不会显示出来
      splashHandler.sendMessageDelayed(msg, SPLASH_TIME);
}

}

你可能感兴趣的:(APP启动界面SplashActivity的两种实现方法)