Android/wifi判定
提供: 初心者エンジニアの簡易メモ
AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
TestActivity.java
NetworkUtil.checkNetwork(getApplicationContext());
NetworkUtil.java
import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.util.Log; public class NetworkUtil { public static void checkNetwork(Context context) { ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) { Log.i("tag", "wifi"); } else { Log.i("tag", "mobile"); } } else { Log.i("tag", "not connected"); } } }
非推奨コード
getType()とConnectivityManager.TYPE_WIFIは非推奨コード(deprecationが起こる)
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
Android/非推奨コード [ショートカット]