android開發(fā)之歡迎界面的小例子
首先你得寫好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>/**
* 判斷系統(tǒng)的網絡是否可用
* @return
*/
private boolean isNetworkConnected(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info =cm.getActiveNetworkInfo();
if(info!=null&&info.isConnected()){
return true;
}else {
return false ;
}
/**
* 判斷系統(tǒng)的網絡是否可用
* @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 界面。
相關文章
Android Studio報錯unable to access android sdk add-on list解決方案
這篇文章主要介紹了Android Studio報錯unable to access android sdk add-on list解決方案,本文通過多種方式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03Android 動畫之TranslateAnimation應用詳解
本節(jié)講解TranslateAnimation動畫,TranslateAnimation比較常用,比如QQ,網易新聞菜單條的動畫,就可以用TranslateAnimation實現,本文將詳細介紹通過TranslateAnimation 來定義動畫,需要的朋友可以參考下2012-12-12解析Android 8.1平臺SystemUI 導航欄加載流程
這篇文章主要介紹了Android 8.1平臺SystemUI 導航欄加載流程,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-11-11