2013-01-08 74 views
1

在我的應用程序中,我總是通過互聯網連接到openfire服務器。現在,如果連接中斷或斷開連接,我希望它應該彈出一個按鈕,然後按OK按鈕,它應該重新登錄該人員(與連接到服務器的連接丟失 - 即使1秒鐘該人員斷開)。Android後臺服務或接收器在後臺檢查互聯網連接

我米使用下面的代碼:

public class ConnectionCheck extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 

     ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo(); 
     NetworkInfo mobileNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

     if(activeNetInfo!=null){ 
      Toast.makeText(context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT).show(); 
     } 

     if(mobileNetInfo != null) 
      { 
       Toast.makeText(context, "Mobile Network Type : " + mobileNetInfo.getTypeName(), Toast.LENGTH_SHORT).show(); 
      } 

    } 

假設當無線網絡連接斷開,再次連接到移動網絡(但內部用戶沒有連接到服務器)

我應該怎麼做對於相同的情況,即當用戶斷開連接時 - 即使距服務器一秒鐘,它應該彈出一個窗口。

有沒有可能?

感謝

回答

3

-你上面的代碼只檢查你的Android設備的連接切換至無線路由或手機的數據分組業務,但不會檢查如果WiFi真的有互聯網連接工作與否。

-我曾經擊中找到這個解決方案時,我工作的一個項目,我試圖尋找它在計算器的解決方案,但我得到的是一段代碼,就像你的。 所以我創建了我自己的解決方案。

我需要的時候做一些定製work..but得到它和運行...

我的代碼切換來自WiFi移動網絡的關閉。

,我使用的端口37 TimeService知道,互聯網是死的,而wifi連接仍然打開

現在我在這裏把一個完整的工作代碼,我做了。請原諒我作爲DRY(不要重複自己的原則)已被濫用在這裏。所以,請在生產網絡

/////---------------------------Intial Available Network Checking 



private boolean checkConnection(){ 


boolean connected = false; 
ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 

if (cm != null) { 
NetworkInfo[] netInfo = cm.getAllNetworkInfo(); 

for (NetworkInfo ni : netInfo) { 
if ((ni.getTypeName().equalsIgnoreCase("WIFI") 
|| ni.getTypeName().equalsIgnoreCase("MOBILE")) 
& ni.isConnected() & ni.isAvailable()) { 
connected = true; 
    } 

    } 
} 


return connected; 
} /////---------------------------Intial Available Network Checking 

/////使用時重構代碼和重複碼轉換成方法,即到single sensible place,--------------- ----------------檢查工作Internet連接

public boolean inetAddr(){ 

    boolean x1 = false; 


    try { 
     Socket s = new Socket("utcnist.colorado.edu", 37); 

     InputStream i = s.getInputStream(); 

     Scanner scan = new Scanner(i); 

     while(scan.hasNextLine()){ 

      System.out.println(scan.nextLine()); 
      x1 = true; 
     } 
    } catch (Exception e) { 


      x1 = false; 
    } 

    return x1; 

} 

/////-------------------------------Check for the working Internet Connection 


////-------------------------------Check Mobile Conectivity Again 

public boolean mobileConnect(){ 

    boolean conn = false; 
    ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo activeNet = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

    if(activeNet != null){ 

     conn = true; 
    }else{ 

     conn = false; 
    } 

    return conn; 



} 

////------------------------------Check Mobile Conectivity Again 

我在這裏使用上述方法....

try{  
    if (!checkConnection()){ 


     AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(YumZingSplashActivity.this); 
     myAlertDialog.setTitle("--- Connectivity Check ---"); 
     myAlertDialog.setMessage("No Internet Connectivity"); 
     myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 

      public void onClick(DialogInterface arg0, int arg1) { 

      YumZingSplashActivity.this.finish(); 
      //splashHandler.removeCallbacks(launcherRunnable); 

      }}); 
      System.out.println("No Internet Connectivity"); 

      myAlertDialog.show();   




     } 
     else{ 


       if(inetAddr()){ 
      aphandle = APIHandling.getInstance(); 
      aphandle.xmlCreateSession(); 
      System.out.println("Net Connectivity is Present"); 
      DURATION = Integer.valueOf(getString(R.string.splash_duration)); 



      splashHandler = new Handler(); 

      // ================ Main Code of the Application 
      launcherRunnable = new Runnable() { 

       public void run() { 
        Intent i = new Intent(YumZingSplashActivity.this, YumZingTabHostActivity.class); 
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(i); 
        YumZingSplashActivity.this.finish(); 
       } 
      }; 
      if (DEBUG) 
      { 
       splashHandler.post(launcherRunnable); 
      } 
      else{ 


       splashHandler.postDelayed(launcherRunnable, DURATION); 
      } 

     } 
       else{ 

        if(mobileConnect()){ 


         if(inetAddr()){ 
         aphandle = APIHandling.getInstance(); 
         aphandle.xmlCreateSession(); 
         System.out.println("Net Connectivity is Present"); 
         DURATION = Integer.valueOf(getString(R.string.splash_duration)); 



         splashHandler = new Handler(); 

         // ================ Main Code of the Application 
         launcherRunnable = new Runnable() { 

          public void run() { 
           Intent i = new Intent(YumZingSplashActivity.this, YumZingTabHostActivity.class); 
           i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
           startActivity(i); 
           YumZingSplashActivity.this.finish(); 
          } 
         }; 
         if (DEBUG) 
         { 
          splashHandler.post(launcherRunnable); 
         } 
         else{ 


          splashHandler.postDelayed(launcherRunnable, DURATION); 
         } 
         }else{ 

          AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(YumZingSplashActivity.this); 
         myAlertDialog.setTitle("--- Connectivity Check ---"); 
         myAlertDialog.setMessage("No Internet Connectivity"); 
         myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 

          public void onClick(DialogInterface arg0, int arg1) { 

          YumZingSplashActivity.this.finish(); 
          //splashHandler.removeCallbacks(launcherRunnable); 

          }}); 
          System.out.println("No Internet Connectivity"); 

          myAlertDialog.show();  
         } 
        }else{ 




         AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(YumZingSplashActivity.this); 
         myAlertDialog.setTitle("--- Connectivity Check ---"); 
         myAlertDialog.setMessage("No Internet Connectivity"); 
         myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { 

          public void onClick(DialogInterface arg0, int arg1) { 

          YumZingSplashActivity.this.finish(); 
          //splashHandler.removeCallbacks(launcherRunnable); 

          }}); 
          System.out.println("No Internet Connectivity"); 

          myAlertDialog.show();   






        } 

       } 
     } 

    //setContentView(R.layout.yumzing_splash_layout); 
    } catch(Exception ex){ 

      System.out.println("Leak ko catch"); 
     } 



    } 
+0

我應該在哪裏寫代碼?在廣播接收機? –

+0

@GauravArora @GauravArora我通常在通過互聯網撥打電話之前使用此代碼,因此您可以在那裏使用它以防止不必要的開銷,或者如果您希望可以在廣播接收器中使用此代碼,但那會稍微重一點..... –

+1

但我的情況下,如果不同的兄弟。我想檢查所有的時間,因爲我的應用程序總是連接到服務器。如果它沒有連接到服務器,那麼我必須彈出一個彈出框 –

2

你也可以試試這個服務類。只需設置以秒爲單位的時間間隔和URL到平:

Android Check Internet Connection

只要記住服務添加到清單文件,並添加權限