facebook twitter hatena line email

「Android/wifi判定」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
行1: 行1:
 
AndroidManifest.xml
 
AndroidManifest.xml
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 
TestActivity.java
 
TestActivity.java
        Context context = getApplicationContext();
+
Context context = getApplicationContext();
        ConnectivityManager connMgr = (ConnectivityManager)
+
ConnectivityManager connMgr = (ConnectivityManager)
        context.getSystemService(Context.CONNECTIVITY_SERVICE);
+
context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
+
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
+
if (networkInfo != null && networkInfo.isConnected()) {
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
+
    if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                Log.i("tag", "wifi");
+
        Log.i("tag", "wifi");
            } else {
+
    } else {
                Log.i("tag", "mobile");
+
        Log.i("tag", "mobile");
            }
+
    }
        } else {
+
} else {
 
             Log.i("tag", "not connected");
 
             Log.i("tag", "not connected");
        }
+
}

2016年10月14日 (金) 12:06時点における版

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");
}