android开发之欢迎界面的小例子
首先你得写好xml文件,这也是最主要的。
然后,在activity中加入一个线程,延时2秒,用来跳转到主界面。
activity中线程代码如下:(顺便检测一下网络是否打开)
[java]
@override
protected void onstart() {
super.onstart();
if(<span style="color: #ff0000">isnetworkconnected()</span>){
new thread(){
@override
public void run() {
try {
thread.sleep(2000);
intent intent = new intent(<span style="color: #ff0000">splashactivity.this</span>,<span style="color: #ff0000">compusassistmain.class</span>);
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(<span style="color: #ff6666">"com.android.settings"</span>, <span style="color: #ff6666">"com.android.settings.wirelesssettings"</span>);
startactivity(intent);
}
});
builder.setnegativebutton("取消", new onclicklistener() {
public void onclick(dialoginterface dialog, int which) {
finish();
}
});
builder.create().show();
}
}
@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():
[java]
<span style="white-space: pre"> </span>/**
* 判断系统的网络是否可用
* @return
*/
private boolean isnetworkconnected(){
connectivitymanager cm = (connectivitymanager) getsystemservice(connectivity_service);
networkinfo info =cm.getactivenetworkinfo();
if(info!=null&&info.isconnected()){
return true;
}else {
return false ;
}
/**
* 判断系统的网络是否可用
* @return
*/
private boolean isnetworkconnected(){
connectivitymanager cm = (connectivitymanager) getsystemservice(connectivity_service);
networkinfo info =cm.getactivenetworkinfo();
if(info!=null&&info.isconnected()){
return true;
}else {
return false ;
}
这样就完成了一个欢迎界面,给自已的应用加点色彩。当然还要添加配置在manifest文件中
[html]
<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>
<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>
这样它就会打开应用,启动第一个activity 界面。
上一篇: PHP实现的简单网络硬盘
下一篇: php类自动加载器实现方法
推荐阅读