在开发自己的app时,经常在进入主界面之前需要写一个简单的SplashActivity欢迎界面,大概持续3、4秒钟之后再跳转到主界面。以下是本人的一个例子,可以参考。当点击打开app时,有一个欢迎界面,持续3秒后跳转到主界面(HelloWorld)详细的注释在代码里。
代码如下:
AndroidManifest.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.leidong.mobilesafe">
<application
android:allowBackup="true"
android:icon="@drawable/appicon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--欢迎界面-->
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--主界面-->
<activity
android:name=".TestActivity">
</activity>
</application>
</manifest>
SplashActivity.xml
package com.example.leidong.mobilesafe;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by leidong on 2016/10/5.
*/
public class SplashActivity extends AppCompatActivity {
private TextView tv_splash_version;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置为无标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置为全屏模式
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);
//获取组件
RelativeLayout r1_splash = (RelativeLayout)findViewById(R.id.r1_splash);
tv_splash_version = (TextView)findViewById(R.id.tv_splash_version);
tv_splash_version.setText("版本号:" + getVersion());
//背景透明度变化3秒内从0.3变到1.0
AlphaAnimation aa = new AlphaAnimation(0.3f, 1.0f);
aa.setDuration(3000);
r1_splash.startAnimation(aa);
//创建Timer对象
Timer timer = new Timer();
//创建TimerTask对象
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
Intent intent = new Intent(SplashActivity.this, TestActivity.class);
startActivity(intent);
finish();
}
};
//使用timer.schedule()方法调用timerTask,定时3秒后执行run
timer.schedule(timerTask, 3000);
}
/**
* 获取当前软件版本号
* @return
*/
private String getVersion(){
//得到系统的包管理器,已经得到了apk的面向对象包装
PackageManager pm = this.getPackageManager();
try{
//参数一:当前应用程序的包名;
//参数二:可选的附加信息,这里用不到,可以定义为0
PackageInfo info = pm.getPackageInfo(getPackageName(), 0);
return info.versionName;
}catch (Exception e){//包名未找到异常,理论上,该异常不可能发生
e.printStackTrace();
return "";
}
}
}
package com.example.leidong.mobilesafe;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by leidong on 2016/10/5.
*/
public class TestActivity extends Activity {
public void onCreate(Bundle savedInstateState){
super.onCreate(savedInstateState);
setContentView(R.layout.hello);
}
}
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/r1_splash"
android:layout_width="match_parent"
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.example.leidong.mobilesafe.SplashActivity"
android:background="@drawable/background1">
<!--显示版本号-->
<TextView
android:text="版本号:"
android:textStyle="bold|italic"
android:textSize="40sp"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="48dp"
android:layout_marginEnd="48dp"
android:id="@+id/tv_splash_version"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<!--显示进度条-->
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:id="@+id/progressBar"
android:layout_below="@+id/tv_splash_version"
android:layout_centerHorizontal="true"
android:layout_marginTop="132dp" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="HelloWorld"
android:textSize="30dp"/>
</LinearLayout>
运行效果:
1.欢迎界面
2.主界面