添加網路狀態許可權 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 代碼示例: public void checkNet(View v){ if(isNetworkConnected(this)){ T
添加網路狀態許可權
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
代碼示例:
public void checkNet(View v){ if(isNetworkConnected(this)){ Toast.makeText(this, "網路可用",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this, "網路不可用",Toast.LENGTH_SHORT).show(); } } //檢查網路狀態 public boolean isNetworkConnected(Context context){ if(context!=null){ ConnectivityManager mConnectivityManager=(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo=mConnectivityManager.getActiveNetworkInfo(); if(mNetworkInfo!=null){ return mNetworkInfo.isAvailable(); } } return false; }