facebook twitter hatena line google mixi email

Android/wifi判定

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

AndroidManifest.xml

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

TestActivity.java

Context context = getApplicationContext();
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");
}