import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.AlphaAnimation;
import android.widget.LinearLayout;
import android.widget.TextView;
public class SplashActivity extends Activity {
private TextView versionNumber;
private LinearLayout mLinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mLinearLayout = (LinearLayout) this.findViewById(R.id.LinearLayout01);
versionNumber = (TextView) this.findViewById(R.id.versionNumber);
versionNumber.setText(getVersion());// 设置版本号
// 判断当前网络状态是否可用
if (isNetWorkConnected()) {
// splash 做一个动画,进入主界面
AlphaAnimation alpha = new AlphaAnimation(0.5f, 1.0f);
alpha.setDuration(3000);
mLinearLayout.setAnimation(alpha);
mLinearLayout.startAnimation(alpha);
// 通过handler 延时3秒 执行Runnable任务
new Handler().postDelayed(new LoadMainTabTask(), 3000);
} else {
showSetNetworkDialog();
}
}
private class LoadMainTabTask implements Runnable {
public void run() {
Intent intent = new Intent(SplashActivity.this,
MainActivity.class);
startActivity(intent);
finish();
}
}
/**
* 对话框提示用户设置网络
* */
private void showSetNetworkDialog() {
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("设置网络");
builder.setMessage("网络错误请检查网络状态");
builder.setPositiveButton("设置网络", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
// 类名一定要包含包名
intent.setClassName("com.android.settings",
"com.android.settings.WirelessSettings");
startActivity(intent);
finish();
}
});
builder.setNegativeButton("取消", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.create().show();
}
/**
* 获取版本号
* */
private String getVersion() {
try {
PackageInfo info = getPackageManager().getPackageInfo(
getPackageName(), 0);
return "版本号: " + info.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
return "Version";
}
}
/**
* 判断网络状态
*/
private boolean isNetWorkConnected() {
ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
return (info != null && info.isConnected());
}
}
转载于:https://my.oschina.net/spoon2014/blog/423757