Android wifi信号强弱检测

  • 效果图
    Android wifi信号强弱检测_第1张图片

  • 目录结构
    Android wifi信号强弱检测_第2张图片

  • AndroidManifest.xml中加入权限声明

    
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  • 布局文件 activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff111627">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/wifi_0" />

RelativeLayout>
  • MainActivity.java
public class MainActivity extends AppCompatActivity {

    private ImageView mImageView;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                checkWifiState();
                sendEmptyMessageDelayed(0, 2000);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = (ImageView) findViewById(R.id.image);

        mHandler.sendEmptyMessageDelayed(0, 2000);
    }

    /**
     * 检查wifi是否处开连接状态
     * @return
     */
    public boolean isWifiConnect() {
        ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifiInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        return mWifiInfo.isConnected();
    }

    /**
     * 检查wifi强弱并更改图标显示
     */
    public void checkWifiState() {
        if (isWifiConnect()) {
            WifiManager mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            WifiInfo mWifiInfo = mWifiManager.getConnectionInfo();
            int wifi = mWifiInfo.getRssi();//获取wifi信号强度
            if (wifi > -50 && wifi < 0) {//最强
                mImageView.setImageResource(R.drawable.wifi_2);
            } else if (wifi > -70 && wifi < -50) {//较强
                mImageView.setImageResource(R.drawable.wifi_1);
            } else if (wifi > -80 && wifi < -70) {//较弱
                mImageView.setImageResource(R.drawable.wifi_0);
            } else if (wifi > -100 && wifi < -80) {//微弱
                mImageView.setImageResource(R.drawable.wifi_0);
            }
        } else {
            //无连接
            mImageView.setImageResource(R.drawable.wifi_null);
        }
    }
}

  • 实现思路
    1. 判断是否是wifi连接,获取系统的服务getSystemService(Context.CONNECTIVITY_SERVICE),得到NetworkInfo对象,调用该对象的isConnected()方法。
    2. 检查wifi信号的强弱,获取WifiManager对象,得到WifiInfo对象,调用getRssi()方法获取wifi信号的强度。
    3. 使用Handler每隔2000毫秒检查wifi状态,更新状态图标。

你可能感兴趣的:(Android)