「Android/wifi判定」の版間の差分
提供: 初心者エンジニアの簡易メモ
行1: | 行1: | ||
AndroidManifest.xml | AndroidManifest.xml | ||
− | + | <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
TestActivity.java | 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"); | 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"); }