Android判斷設備是否打開WIFI、GPRS數據連接代碼摘要:
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
ConnectivityManager cm;
cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
boolean isWifiConnected=cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED ? true : false ;
boolean isGprsConnected=cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED ? true : false ;
需要添加權限:
<!-- 查詢網絡狀態權限 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 修改手機連接網路狀態權限 -->
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11