时间:2020-10-04 14:42:53 | 栏目:Android代码 | 点击:次
首先你得写好xml文件,这也是最主要的。
然后,在activity中加入一个线程,延时2秒,用来跳转到主界面。
activity中线程代码如下:(顺便检测一下网络是否打开)
[java]
@Override
protected void onStart() {
super.onStart();
if(isNetworkConnected()){
new Thread(){
@Override
public void run() {
try {
Thread.sleep(2000);
Intent intent = new Intent(SplashActivity.this,CompusAssistMain.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}else{
//弹出对话框 让用户设置网络
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);
}
});
builder.setNegativeButton("取消", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.create().show();
}
}检测网络的类isNetWorkConnected():
/**
* 判断系统的网络是否可用
* @return
*/
private boolean isNetworkConnected(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info =cm.getActiveNetworkInfo();
if(info!=null&&info.isConnected()){
return true;
}else {
return false ;
}
<activity
android:name="com.yan.compusassist.SplashActivity"
android:label="@string/application_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>