2014-12-31 198 views
0

繼承人我的代碼用於檢查我的應用程序的網絡連接。我希望我的應用程序只在連接到網絡時運行,如果不是,則關閉它。代碼運行時沒有錯誤,但問題在於alertdialog顯示很多次。檢查網絡連接android

private void checkConnectivity(){ 

     final Thread checkConnection = new Thread(new Runnable(){ 
      @Override 
      public void run() 
      { 

       while (checkCon == true){ 
       if(!isNetworkAvailable(MainActivity.this)) { 

        runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
          // TODO Auto-generated method stub 
          new AlertDialog.Builder(MainActivity.this) 
          .setMessage("No network connection.") 
          .setCancelable(false) 
          .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface d, int which) {         
            checkCon = false; 
            finish(); 

           } 
          }).show();               


         } 
        });       
        } else { 
         checkCon = true; 
        } 

      try { 
       Thread.sleep(2000); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
       }} 
     }); 
     checkConnection.start(); 


    } 
+0

爲什麼你使用'while'循環? –

+0

關閉線程 –

+0

如果您在顯示/創建對話框(不僅在用戶關閉對話框之後)設置了_checkCon = false_,則從代碼中看起來似乎最簡單。目前,循環似乎不斷重新創建對話框,直到用戶關閉其中一個對話框。 – harism

回答

0

通過harism解答,非常感謝

private void checkConnectivity(){ 

     final Thread checkConnection = new Thread(new Runnable(){ 
      @Override 
      public void run() 
      { 

       while (checkCon == true){ 
       if(!isNetworkAvailable(MainActivity.this)) { 

        runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
          // TODO Auto-generated method stub 
          new AlertDialog.Builder(MainActivity.this) 
          .setMessage("No network connection.") 
          .setCancelable(false) 
          .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface d, int which) {         

            finish();          
           } 

          }).show(); 
          checkCon = false; 
         } 
        });       
        } else { 
         checkCon = true; 
        } 

      try { 
       Thread.sleep(2000); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
       }} 
     }); 
     checkConnection.start(); 


    } 
0

添加一個新的方法中您當前ActivityFragment

private boolean isNetworkAvailable(){ 
     boolean available = false; 
     /** Getting the system's connectivity service */ 
     ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 

     /** Getting active network interface to get the network's status */ 
     NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 

     if(networkInfo !=null && networkInfo.isAvailable()) 
      available = true; 

     /** Returning the status of the network */ 
     return available; 
    } 

這裏是如何使用它。你可以在裏面使用它onCreate()方法:

if (isNetworkAvailable() == true){ // if network is available 
    // do your thing here 
    ... 
}else{ // "else" means that the network is not available 
    // do your thing here 
    ... 
}