0

有可能吗?我有一个活动和一个警报对话框。但我需要先运行活动,然后 2 秒后出现警报对话框。我不知道怎么做。问候

PD:我不是说英语的人

public class Pantalladeinicio extends Activity {

private final int SPLASH_DISPLAY_LENGTH = 2000; 

 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE); 
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
  setContentView(R.layout.index);


  if(checkInternetConnection()==true) {

  new Handler().postDelayed(new Runnable() {

   public void run() {
    Intent mainIntent = new Intent(Pantalladeinicio.this,
      NetworkingActivity.class);
    mainIntent.putExtra("MAIN", true);
    startActivity(mainIntent);
    finish();
   }
  }, SPLASH_DISPLAY_LENGTH); 

  } 

  else
  {
         AlertDialog.Builder dialogo1 = new AlertDialog.Builder(this);  
            dialogo1.setTitle("Importante");  
            dialogo1.setMessage("Debe activar la Conexion a Internet");            
            dialogo1.setCancelable(false);  
            dialogo1.setPositiveButton("Aceptar", new  DialogInterface.OnClickListener() { 


                public void onClick(DialogInterface dialogo1, int id) {  

                    aceptar();  
                }  
            }); 
            dialogo1.show();
      Log.v("TAG", "Internet Connection Not Present");


  }






 }


 public void aceptar() {
       // Toast t=Toast.makeText(this,"Bienvenido a probar el programa.", Toast.LENGTH_SHORT);

     super.onDestroy();
      finish();

    }



 private boolean checkInternetConnection() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        // test for connection
        if (cm.getActiveNetworkInfo() != null
                && cm.getActiveNetworkInfo().isAvailable()
                && cm.getActiveNetworkInfo().isConnected()) {
            return true;
        } else {
            //Log.v("TAG", "Internet Connection Not Present");

            return false;
        }
    }

}

4

3 回答 3

1

我不明白您的问题,但查看接受的答案,我建议更改现有代码的顺序:

new Handler().postDelayed(new Runnable() {
    public void run() {
        if(checkInternetConnection()) {
            Intent mainIntent = new Intent(Pantalladeinicio.this, NetworkingActivity.class);
            mainIntent.putExtra("MAIN", true);
            startActivity(mainIntent);
            finish();
        } 
        else
        {
            AlertDialog.Builder dialogo1 = new AlertDialog.Builder(this);  
            dialogo1.setTitle("Importante");  
            dialogo1.setMessage("Debe activar la Conexion a Internet");            
            dialogo1.setCancelable(false);  
            dialogo1.setPositiveButton("Aceptar", new  DialogInterface.OnClickListener() { 
                public void onClick(DialogInterface dialogo1, int id) {  
                    aceptar();  
                }  
            }); 
            dialogo1.show();
            Log.v("TAG", "Internet Connection Not Present");
        }
    }
}, SPLASH_DISPLAY_LENGTH); 
于 2012-11-30T20:38:07.450 回答
0

将您的代码更改为使用ThreadrunOnUiThread

  //Your Code here...
    else
      {
    myThread();
      }
    }
    public void myThread(){
        Thread th=new Thread(){

         @Override
         public void run(){
          try
          {

           Thread.sleep(2000);
           Pantalladeinicio.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
             // TODO Auto-generated method stub
                AlertDialog.Builder dialogo1 = new AlertDialog.Builder(Pantalladeinicio.this);  
                dialogo1.setTitle("Importante");  
                dialogo1.setMessage("Debe activar la Conexion a Internet");            
                dialogo1.setCancelable(false);  
                dialogo1.setPositiveButton("Aceptar", new  DialogInterface.OnClickListener() { 


                    public void onClick(DialogInterface dialogo1, int id) {  

                        aceptar();  
                    }  
                }); 
                dialogo1.show();
          Log.v("TAG", "Internet Connection Not Present");
               }
           });

          }catch (InterruptedException e) {
        // TODO: handle exception
       }
    }
        };
        th.start();
       }
于 2012-11-30T20:00:28.733 回答
0

我假设您想在进入 else 块后等待 2 秒钟。您可以通过在调用Thread.sleep(2000);之前调用来做到这一点dialogo1.show();。但是,这会使您的程序看起来“冻结”。为了避免这种情况,您可以创建一个单独的线程来休眠然后显示对话框。

于 2012-11-30T20:00:56.160 回答