android开发之欢迎界面的小例子

2019/7/7 20:19:11

本文主要是介绍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 界面。


这篇关于android开发之欢迎界面的小例子的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程