Android
 Computer >> コンピューター >  >> プログラミング >> Android

Androidで現在のWi-Fiリンク速度を取得する方法を解説

この記事では、Androidアプリで現在のWi-Fiリンク速度を取得して画面に表示する方法を、ステップごとにわかりやすく解説します。

手順1:新しいプロジェクトを作成する

Android Studioを開き、「File」→「New Project」を選択して、必要な情報を入力し、新しいプロジェクトを作成します。

手順2:レイアウトファイル(activity_main.xml)を編集する

res/layout/activity_main.xml に以下のコードを追加します。

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android"
   xmlns:app = "https://schemas.android.com/apk/res-auto"
   xmlns:tools = "https://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:gravity = "center"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity">
   <TextView
      android:id = "@+id/text"
      android:textSize = "30sp"
      android:layout_width = "match_parent"
      android:layout_height = "match_parent" />
</LinearLayout>

上記のコードでは、Wi-Fiのリンク速度を表示するためのTextViewを配置しています。

手順3:MainActivity.java にコードを追加する

src/MainActivity.java に以下のコードを記述します。

package com.example.myapplication;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Formatter;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
   TextView textView;
   @RequiresApi(api = Build.VERSION_CODES.N)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      textView = findViewById(R.id.text);
      WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
      WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
      textView.setText("" + wifiInfo.getLinkSpeed());
   }
   @Override
   protected void onStop() {
      super.onStop();
   }
   @Override
   protected void onResume() {
      super.onResume();
   }
}

ここでのポイントは、WifiManagerのインスタンスを取得し、getConnectionInfo()メソッドでWifiInfoオブジェクトを取得した後、getLinkSpeed()メソッドを呼び出すことで、現在のWi-Fiリンク速度(Mbps単位)を取得できる点です。

手順4:AndroidManifest.xml に権限を追加する

androidManifest.xml に以下のコードを追加します。

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "https://schemas.android.com/apk/res/android"
    package = "com.example.myapplication">
    <uses-permission android:name = "android.permission.ACCESS_WIFI_STATE" />
    <application
        android:allowBackup = "true"
        android:icon = "@mipmap/ic_launcher"
        android:label = "@string/app_name"
        android:roundIcon = "@mipmap/ic_launcher_round"
        android:supportsRtl = "true"
        android:theme = "@style/AppTheme">
        <activity android:name = ".MainActivity">
            <intent-filter>
                <action android:name = "android.intent.action.MAIN" />
                <action android:name = "android.net.conn.CONNECTIVITY_CHANGE" />
                <category android:name = "android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Wi-Fiの状態情報にアクセスするために、ACCESS_WIFI_STATEパーミッションの宣言が必須となります。これを忘れるとアプリが正常に動作しないので注意してください。

アプリを実行してみよう

それでは、実際にアプリケーションを実行してみましょう。Android端末をパソコンに接続していることを前提とします。Android Studioから実行するには、プロジェクト内のいずれかのアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックします。接続済みのモバイルデバイスを選択すると、実機の画面にWi-Fiリンク速度が表示されます。

Androidで現在のWi-Fiリンク速度を取得する方法を解説

  1. Androidで現在のスレッドがデーモンかどうかを確認する方法【サンプルコード付き】

    はじめに:スレッドとデーモンスレッドとはスレッドとは、軽量なサブプロセスのことであり、UI(ユーザーインターフェース)を妨げることなくバックグラウンド処理を実行する仕組みです。本記事では、Androidアプリにおいて現在のスレッドがデーモンスレッドかどうかを確認する方法を、実際のサンプルコードとともにわかりやすく解説します。なお、デーモンスレッドとは、JVM(Java仮想マシン)の終了を待たずに動作するバックグラウンドスレッドのことです。UIを担当するメインスレッドは、通常デーモンには設定されていません。手順1:新しいプロジェクトを作成するまず、Android Studioで新しいプロジェクト

  2. 【Android】現在のスレッドIDを取得する方法を解説

    本記事では、Androidアプリで現在実行中のスレッドIDを取得する方法を、サンプルコードとともに解説します。スレッドとは?例に入る前に、まず「スレッド」について簡単におさらいしておきましょう。スレッドとは、軽量なサブプロセス(処理の単位)のことであり、UIの動作を妨げることなくバックグラウンドで処理を実行するために使われます。メインスレッドとは別に動作させることで、画面の描画やユーザー操作をブロックせずに、重い処理を並行して行うことが可能になります。このサンプルでは、Thread.currentThread().getId() を呼び出すことで、現在実行中のスレッドIDを取得し、TextVi