2014-11-25 99 views
1

我目前正在做一個基於網絡連接的項目。我正在開發一個應用程序,其中定期檢查網絡連接。如果沒有連接,進度對話框應該旋轉顯示「沒有網絡連接「直到用戶自己打開wifi或任何其他類型的互聯網連接。在wifi打開之後,如果應用程序連接到wifi,則進度對話框應該關閉,並且控制應該傳遞給另一活動。我在谷歌搜索了很多次,但沒有得到滿意的答案。下面是我的代碼:當檢測到網絡連接時開始另一個活動

 public class Alert extends Activity implements Runnable{ 


     ProgressDialog pd; 
    WifiManager wm,wifiManager; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_alert); 


     wm = (WifiManager) getSystemService(WIFI_SERVICE); 

     if(!wm.isWifiEnabled()) { 
     pd = ProgressDialog.show(this, "Network Error!!", "Network not found.Please make sure there is presence of any kind of network connection!!"); 

     Thread t = new Thread(this); 
     t.start(); 
     } 
    } 

    @Override 
    protected void onResume() { 

     super.onResume(); 

     if(wm.isWifiEnabled()) { 
      pd.dismiss(); 
    Intent in=new Intent(Alert.this,WebPageView.class); 
      startActivity(in); 

     } 
    } 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 

      while(wm.getWifiState() != 3) { 

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

      } 

      } 
    } 
} 

回答

0

嘗試這樣使用處理程序,它可能工作。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_alert); 


     wm = (WifiManager) getSystemService(WIFI_SERVICE); 

     Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
     public void run() { 
     if(!wm.isWifiEnabled()) { 
     runOnUiThread(new Runnable() { 
       public void run() { 
       if(pd == null) 
        pd = ProgressDialog.show(this, "Network Error!!", "Network not found.Please make sure there is presence of any kind of network connection!!"); 
       } 
      }); 

     }else{ 
     runOnUiThread(new Runnable() { 
       public void run() { 
       if(pd != null) 
        pd.dismiss(); 
       Intent in=new Intent(Alert.this,WebPageView.class); 
       startActivity(in); 
       } 
      }); 


     } 
     handler.postDelayed(this, 5000); //now is every 2 minutes 
     } 
    }, 5000); 

    } 
+0

在5秒的間隔,它不斷崩潰,並打開自己,並再次崩潰,如在一個循環 – 2014-11-25 07:20:24

+0

我編輯我的答案可以檢查它並重播我。 – 2014-11-25 07:25:33

+0

感謝他的工作.. :) – 2014-11-25 08:31:43

0

檢查這個link

取決於網絡狀態的改變,你可以打電話給你的活動。

希望這將解決您的問題

+0

似乎更像是評論,而不是回答 – 2014-11-25 06:45:50

+0

他正在定期檢查互聯網或其他網絡。他可以立即檢查互聯網狀態。這是檢查互聯網狀態的正確方法。那麼他可以調用具體的活動。 – Jeeva 2014-11-25 06:52:02

0
@Override 
public void run() { 
    // TODO Auto-generated method stub 

    while(running) { 

     try { 
    ConnectivityManager cm = (ConnectivityManager) 
    context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo info = cm.getActiveNetworkInfo(); 
     if (info != null) { 
      if (info.isConnected()) { 
       // you got a connection! tell your user! 
       Log.i("Post", "Connected"); 
       running=false; 
    runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
    // TODO Auto-generated method stub 
    pd.dismiss(); 
     } 
     }); 
       Intent intent_service=new Intent(this, anotherActivity.class); 
       context.startService(intent_service); 

      } 

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

    } 

    } 
0

把你的項目這個類:

public class Utility { 
    public static boolean isNetworkConnected(Context context){ 
     boolean connected = false; 
     ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
     connected = (cm.getActiveNetworkInfo() != null&&cm.getActiveNetworkInfo().isAvailable() && cm 
      .getActiveNetworkInfo().isConnected()); 
     return connected; 
    } 
    public static void showAlert(final Activity activity, final String message,final String title) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(activity); 
     builder.setMessage(message).setTitle(title).setCancelable(false) 
      .setNegativeButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog,int id){ 
        dialog.cancel(); 
        activity.finish(); 
       } 
      }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 
    public static void showAlertValidation(final Activity activity,final String message,final String title){ 
     AlertDialog.Builder builder = new AlertDialog.Builder(activity); 
     builder.setMessage(message).setTitle(title).setCancelable(false) 
      .setNegativeButton("OK", new DialogInterface.OnClickListener(){ 
       public void onClick(DialogInterface dialog,int id){ 
        dialog.cancel(); 
       } 
      }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } 
} 

此類檢測,如果你的網絡連接是與否。之後,在您的活動,把下面的代碼:

if(Utility.isNetworkConnected(Alert.this)) 
    { 
Intent in=new Intent(Alert.this,WebPageView.class); 
      startActivity(in); 
} 
      else if(!Utility.isNetworkConnected(Alert.this)) 
       Utility.showAlert(Alert.this,"Internet Connection Not Present.","Network Error!"); 

檢查,看看你的問題得到解決或沒有。

1
To check network connection, use ConnectivityManager class. 
Add below method to your activity and call it to check network connection, if it returns true then it means network is available otherwise not. 

private boolean isNetworkAvailable(Context context) { 
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
    return activeNetworkInfo != null && activeNetworkInfo.isConnected(); 
} 

Now do it in your onCreate(...) method 

if(!isNetworkAvailable(Alert.this)) { 
    pd = ProgressDialog.show(this, "Network Error!!", "Network not found.Please make sure there is presence of any kind of network connection!!"); 
    Thread t = new Thread(this); 
    t.start(); 
}else{ 
    if(pd != null && pd.isShowing()){ 
     pd.dismiss(); 
    } 
    Intent in=new Intent(Alert.this,WebPageView.class); 
    startActivity(in); 
} 

Hope this will help you.