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

前端技术 2023/09/01 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():

本文地址:https://www.stayed.cn/item/2427

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。