在实际工作中,常常遇到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>

运行截图:


更多相关文章

  1. Java 网络 IO 模型
  2. Java使用socket网络编程实现多人聊天室
  3. 20155320 《Java程序设计》实验五网络编程与安全实验报告
  4. Android 网络框架学习之Retrofit
  5. Android中获取网络天气数据
  6. 关于Android4.0之上的ListView显示从网络上获取图片和文字
  7. nor current process has android.permission.WRITE_APN_SETTING
  8. Android功能模块化之网络连接状态判断
  9. android获取网络数据

随机推荐

  1. Java普通代码块,构造代码块,静态代码块区别
  2. Java操作数据库之jdbc【原生方式】
  3. java数据结构--链表
  4. 读阿里巴巴Java开发手册v1.2.0之工程结构
  5. Ubuntu12 64位 阿里云服务器端配置mysql+
  6. 记一次java内存分析
  7. 【java开发系列】—— struts2简单入门示
  8. 被老板 "宠着" 是好是坏???
  9. Java使用Velocity模板发送HTML格式邮件并
  10. 浅析java中equals与==的用法