使用Splash的目的不光是使界面友好,而且能在程序启动时在后台进行一系列动作,如初始化数据,加载文件,检测网络连接,检测新版本等。
因为是启动应用的首个界面,在Manifest文件中注册,intent-filter中使其作为app的启动activity。
activity>
<activity
android:name=".SplashActivity"
android:theme="@android:style/Theme.NoTitleBar"
android:label="@string/title_activity_splash" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
intent-filter>
activity>
SplashActivity类的内部实现。这里MainActivity为跳转后的Acitivity。
使用Handler的postDelayed方法和runnable进行延迟跳转。
public class SplashScreen extends Activity {
private final int SPLASH_DELAY_TIME = 3000;
Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
handler.postDelayed(runnable, SPLASH_DELAY_TIME);
}
Runnable runnable = new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(SplashScreen.this, MainActivity.class);
startActivity(intent);
}
};
}
在res下新建一个文件夹anim用来存储自定义动画(anime)。
<set xmlns:android="http://sandroid:chemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1"
android:toAlpha="0"
android:duration="500" />
set>
在startActivity()方法之后用overridePendingTransition()方法覆盖跳转动画。
overridePendingTransition(R.anim.abc_fade_in, R.anim.fade_out);
在SplashScreen的layout文件中添加一个名为Version的TextView,来显示APP当前版本。
protected String getVersion() {
String versionStr = "";
PackageManager packManger = getPackageManager();
try {
PackageInfo info = packManger.getPackageInfo(getPackageName(), 0);
versionStr = info.versionName;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
versionStr = "";
e.printStackTrace();
}
return versionStr;
}
添加权限
INTERNET
权限ACCESS_NETWORK_STATE
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
...
<application...
自定义ConnectionDetector类。
public class ConnectionDetector {
private Context context;
public ConnectionDetector(Context context) {
this.context = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(connectivity!=null){
NetworkInfo info[] = connectivity.getAllNetworkInfo();
if(info!=null){
for(int i = 0; i < info.length; i++){
if(info[i].getState() == NetworkInfo.State.CONNECTED)
return true;
}
}
}
return false;
}
}
一个checkNetwork的Runnable。
Runnable checkNetwork = new Runnable(){
@Override
public void run() {
ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
NETWORK_STATUS = cd.isConnectingToInternet();
}
};
并在跳转到主界面时通过Bundle传送检测结果,并在首界面显示网络状态提示的Toast。
public class MainActivity extends Activity {
private boolean NETWORK_STATUS = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle bundle = this.getIntent().getExtras();
NETWORK_STATUS = bundle.getBoolean("NETWORK_STATUS");
String message = null;
if(NETWORK_STATUS){
message = "网络已连接";
}else{
message = "网络不可用";
}
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
参考文章:
Android开发学习笔记-splash画面的显示
Android学习系列(35)–App应用之启动界面SplashActivity
如何检查Android网络连接状态