Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> android開發之歡迎界面的小例子

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

編輯:Android開發實例

首先你得寫好xml文件,這也是最主要的。

然後,在activity中加入一個線程,延時2秒,用來跳轉到主界面。

activity中線程代碼如下:(順便檢測一下網絡是否打開)

[java]

代碼如下:

@Override
    protected void onStart() {
        super.onStart();
        if(<SPAN >isNetworkConnected()</SPAN>){
            new Thread(){
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                        Intent intent = new Intent(<SPAN >SplashActivity.this</SPAN>,<SPAN >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 >"com.android.settings"</SPAN>, <SPAN >"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 > </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 界面。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved