延迟警报

时间:2012-11-30 19:51:43

标签: android android-alertdialog

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

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;
        }
    }

}

3 个答案:

答案 0 :(得分: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); 

答案 1 :(得分: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();
       }

答案 2 :(得分:0)

我假设你想进入else块后等待2秒钟。 您可以在致电Thread.sleep(2000);之前致电dialogo1.show();来执行此操作。但是,这会使您的程序显示为“冻结”。为了避免这种情况,你可以创建一个单独的线程来休眠,然后显示对话框。