facebook twitter hatena line email

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

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「TestActivity.java Context context = getApplicationContext(); ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context....」)
 
行1: 行1:
 +
AndroidManifest.xml
 +
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 
TestActivity.java
 
TestActivity.java
 
         Context context = getApplicationContext();
 
         Context context = getApplicationContext();

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

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