2014-09-03 17 views
0

我很新鲜这个android的东西,所以我有点困惑与一些概念仍然。我想要做的是,当我在我的启动画面中显示AlertDialog,当用户没有连接到互联网。我试图用很多方式来做到这一点,但我永远无法保持线程,所以我总是得到一个异常,因为在关闭对话框之前活动关闭了。我曾尝试与处理程序,但没有成功做到这一点...我的代码部分是在这里:创建AlertDialog,同时拿着一个线程

public class Splash extends Activity { 

public void openDialog() { 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
      this); 

     // set title 
     alertDialogBuilder.setTitle(R.string.app_name); 

     // set dialog message 
     alertDialogBuilder 
      .setMessage(R.string.dialogo_net) 
      .setCancelable(false) 
      .setPositiveButton("Sair",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        Splash.this.finish(); 
       } 
       }) 
      .setNegativeButton("Definições",new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id) { 
        Intent intent = new Intent(Settings.ACTION_SETTINGS); 
        intent.addCategory(Intent.CATEGORY_LAUNCHER);   
        startActivity(intent); 
       } 
      }); 

      AlertDialog alertDialog = alertDialogBuilder.create(); 

      alertDialog.show(); 
     } 

Handler mHandler = new Handler() 
{ 
    public void handleMessage(Message msg) 
    { 
     openDialog();//Display Alert 
    } 
}; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.splash); 


    Thread logoTimer = new Thread() { 
     public void run(){ 
      try{ 
       int logoTimer = 0; 
       while(logoTimer < 2000){ 
        sleep(100); 
        logoTimer = logoTimer +100; 
       }; 
       ConnectivityManager cm = 
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
       NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
      if (netInfo == null || !netInfo.isConnectedOrConnecting()) { 
       //opens the dialog in this case 
      mHandler.sendEmptyMessage(0); 
       } else { 
       //goes to main activity 
       startActivity(new Intent("pt.aeist.mobile.START")); } 
      } 

      catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      finally{ 
       finish(); 
      } 
     } 
    }; 

    logoTimer.start(); 

}

}

回答

0
Thread logoTimer = new Thread() 
{ 
    public void run(){ 
     try{ 
      sleep(2000); 
      ConnectivityManager cm = 
       (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
      NetworkInfo netInfo = cm.getActiveNetworkInfo(); 

      if (netInfo == null || !netInfo.isConnectedOrConnecting()) 
      {    
       mHandler.sendEmptyMessage(0); 
      } else 
      { 
      startActivity(new Intent("pt.aeist.mobile.START")); } 
      finish(); 
      } 
     }catch(Exception e) 
     { 
      e.printStackTrace(); 
     }  
    } 
}; 
logoTimer.start(); 
+0

它仍然显示出来,然后随着WindowLeaked异常消失 – 2014-09-03 13:14:14

0

//手柄完成()分开。

.setNegativeButton("Definições",new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog,int id) { 
       Intent intent = new Intent(Settings.ACTION_SETTINGS); 
       intent.addCategory(Intent.CATEGORY_LAUNCHER);   
       startActivity(intent); 
       finish(); 
      } 
     }); 
0

只是从所有地方删除完成()。并检查它。

你必须确定你想把finish();

相关问题