android開發之歡迎介面的小例子

來源:互聯網
上載者:User

首先你得寫好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 介面。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.