Android监控WIFI和GSM状态并绘制网络强度

这篇博客讲述了如何在Android应用中实现监测WIFI和GSM网络状态,并实时绘制网络信号强度。针对手机网络(2G/3G)和WiFi网络,分别介绍了通过PhoneStateListener监听器和定时获取的方式来获取网络强度。对于高及时性需求,作者推荐使用定时获取的方法。此外,文章还介绍了自定义View并通过重写onDraw方法来绘制类似手机信号条的网络强度图形。

在实际工作中,常常遇到APP显示网络强度的需求。

使用过程中涉及的应用权限如下:

    <!--wifi及网络状态-->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

一、网络强度分为二个部分:

    1、手机网络GSM(2G/3G等)

        GSM需要注册PhoneStateListener监听器,通过监听网络改变,获取手机当前网络的强度。

        /* Update the listener, and start it */
        MyListener = new MyPhoneStateListener();

        Tel = ( TelephonyManager )getSystemService(Context.TELEPHONY_SERVICE);
        Tel.listen(MyListener , PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

    /* Start the PhoneState listener */
    private class MyPhoneStateListener extends PhoneStateListener
    {
      /* Get the Signal strength from the provider,
       * each tiome there is an update
       *从得到的信号强度,每个tiome供应商有更新
       */

        @Override
        public void onSignalStrengthsChanged(SignalStrength signalStrength)
        {
            super.onSignalStrengthsChanged(signalStrength);
            //信号强度换算公式
            int astSignal = -113 + 2*signalStrength.getGsmSignalStrength();
            gsm.setText("GSM 信号强度asu :" + signalStrength.getGsmSignalStrength() +"_dBm :"+astSignal);
        }

    };/* End of private Class */

    2、Wifi网络

        Wifi也可以注册WifiManager.WIFI_STATE_CHANGED_ACTION 广播,来获取Wifi信号强度。

        但该方法不及时!!该方法自行搜索!

        若应用的及时性要求比较高的时,我推荐采用主动定时获取的方式。本文也是使用该方法。

    private void getWifiInfo() {
        WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        if (wifiInfo.getBSSID() != null) {
            //wifi名称
            String ssid = wifiInfo.getSSID();
            //wifi信号强度
            int signalLevel = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 6);
            //wifi速度
            int speed = wifiInfo.getLinkSpeed();
            //wifi速度单位
            String units = WifiInfo.LINK_SPEED_UNITS;
            wifi.setText("wifi_level:"+signalLevel +"_speed:"+speed +"_units:"+units);
            wifiDrawView.setType(signalLevel);
        }
    }


二、绘制网络强度

自定义View,通过重载onDraw方法来达到绘制网络强度的图形。本文是绘制的图标为 手机信号的样式。

设置强度

    myDrawView.setType(t);

   
     public void setType(int type){
     this.type = type;
      postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //把整张画布绘制成白色
        canvas.drawColor(0x00000000);
        Paint paint = new Paint();
        //去锯齿
        paint.setAntiAlias(true);
        paint.setColor(Color.BLUE); //线条填充的颜色
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5); //设置线条的宽度
        Log.i(TAG,"TYPE:" + type);
        if(type <=2){
            //绘制
            paint.setColor(Color.RED);
            canvas.drawLine(dip2px(context, 2), dip2px(context, 14),
                    dip2px(context, 2), dip2px(context, 16),
                    paint);
        }else{
            paint.setColor(Color.GREEN);
            //绘制
            canvas.drawLine(dip2px(context, 2), dip2px(context, 14),
                    dip2px(context, 2), dip2px(context, 16),
                    paint);
            if (type >=3){
                canvas.drawLine(dip2px(context, 7), dip2px(context, 12),
                        dip2px(context, 7), dip2px(context, 16),
                        paint);
                canvas.drawLine(dip2px(context, 12), dip2px(context, 8),
                        dip2px(context, 12), dip2px(context, 16),
                        paint);
            }
            if (type >= 4){
                canvas.drawLine(dip2px(context, 17), dip2px(context, 4),
                        dip2px(context, 17), dip2px(context, 16),
                        paint);
            }

            if (type >=5){
                canvas.drawLine(dip2px(context, 23), dip2px(context, 0),
                        dip2px(context, 23), dip2px(context, 16),
                        paint);
            }
        }
    }



    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

资源文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="模拟信号" />

    <test.android.com.drawdemo.MyDrawView
        android:id="@+id/myDraw"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_below="@id/text1"
        android:layout_marginTop="10dp" />

    <TextView
        android:id="@+id/gsm_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/myDraw"
        android:text="GSM"
        android:textColor="@color/background_floating_material_dark" />

    <TextView
        android:id="@+id/wifi_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/gsm_data"
        android:text="wifi"
        android:textColor="@color/background_floating_material_dark" />

    <test.android.com.drawdemo.MyDrawView
        android:id="@+id/wifiDraw"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_below="@+id/wifi_data"
        android:layout_marginTop="10dp" />

</RelativeLayout>

运行截图:


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值