2012-05-21 54 views
0

我想弄清楚當一個按鈕點擊,它會檢查互聯網連接,如果連接,然後將意圖到另一個頁面。如果不是,它會烘烤一條消息。檢查互聯網連接,當點擊一個按鈕

不知何故,我在這個論壇上看到了很多榜樣,但沒有人像我一樣。誰能建議?

search.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 


       Intent gogogo = new Intent(webview.this, viewPage.class); 
       gogogo.putExtra("result", result); 
       startActivity(gogogo); 
      } 
     }); 

final ConnectivityManager conn_manager = (ConnectivityManager) 
      this.getSystemService(Context.CONNECTIVITY_SERVICE); 


      final NetworkInfo network_info = conn_manager.getActiveNetworkInfo(); 
      if (network_info != null && network_info.isConnected()) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 

我弄清楚很多帖子就像上面的代碼一樣,但是不能有任何想法把我的代碼放在上面。

+0

您正面臨的問題是什麼? –

回答

0

試試這個,

 public class MainActivity extends Activity { 

     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      Button b1=(Button)findViewById(R.id.button1); 
      b1.setOnClickListener(new OnClickListener() { 

       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        checkNetworkConnectivity(); 
       } 
      }); 

     } 

     protected void checkNetworkConnectivity() { 
      // TODO Auto-generated method stub 
      ConnectivityManager connMgr = (ConnectivityManager) 
      this.getSystemService(Context.CONNECTIVITY_SERVICE); 

      final android.net.NetworkInfo wifi = 
      connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 

      final android.net.NetworkInfo mobile = 
      connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

      if(wifi.isAvailable() || mobile.isAvailable()){ 


Intent gogogo = new Intent(webview.this, viewPage.class); 
      gogogo.putExtra("result", result); 
      startActivity(gogogo); 

      } 

      else{ 
       Toast.makeText(this, "No Network Available" , Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 

不要忘了在你的清單文件中添加這些權限,

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
0
private boolean checkInternetConnection() { 
     ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 

     // test for connection 
     if (cm.getActiveNetworkInfo() != null 
       && cm.getActiveNetworkInfo().isAvailable() 
       && cm.getActiveNetworkInfo().isConnected()) { 
      Intent gogogo = new Intent(webview.this, viewPage.class); 
      startActivity(gogogo); 



     } else { 
      //no conection 
      //toast 

      return false; 
     } 
    } 


try this. 
1

使用此代碼來檢查,如果互聯網連接被發現或不:

public boolean isNetworkAvailable() { 
    ConnectivityManager connMgr = (ConnectivityManager) getInstance() 
        .getSystemService(Context.CONNECTIVITY_SERVICE); 

    NetworkInfo wifi = connMgr 
        .getNetworkInfo(ConnectivityManager.TYPE_WIFI); 
    NetworkInfo mobile = connMgr 
        .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 

    return (wifi.isConnected() || mobile.isConnected()); 
    } 

添加這些在AndroidManifest文件上執行任務,

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>