一、效果图
一、通过wifimanager来获取WiFi的当前速度状态
WifiManager wm = (WifiManager) NetworkSignalDetectionActivity.this.getApplicationContext().getSystemService(WIFI_SERVICE);
// 获取当前所连接wifi的信息
WifiInfo wi = wm.getConnectionInfo();
int rssi = wi.getRssi();
单次执行此代码可以得到一个int类型的数值,范围为-0- -100+
0- -50代表网速很好
-50- -70代表网速一般
-70- -100代表网速卡顿
-100+代表没有网络,网络中断或者没有打开WiFi
二、我这边是写了一个handle来测试网速的
public Handler handler=new Handler(){
@Override
public void handleMessage(@NonNull @NotNull Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1:
// 获取系统wifi服务
WifiManager wm = (WifiManager) NetworkSignalDetectionActivity.this.getApplicationContext().getSystemService(WIFI_SERVICE);
// 获取当前所连接wifi的信息
WifiInfo wi = wm.getConnectionInfo();
int rssi = wi.getRssi();
Log.e("aaa","Wifi网络数据:"+rssi);
Log.e("aaa" , "数据强度:"+ lastSignal + " dBm");
Log.e("aaa" , "数据强度asu:"+ asu + " asu");
break;
}
}
};
new Thread() {
@Override
public void run() {
while (is_wifi_speed){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 步骤3:创建所需的消息对象
Message msg = Message.obtain();
msg.what = 1;
handler.sendMessage(msg);
}
}
}.start();
数据的网速检测,我是在网上查找的数据网速获取,但是得到的数据一直是固定的85,大家可以参考一下,有经验可以回复一下怎么检测数据的网速状态
这里也是一个测试demo 的代码
private void getMobileNetworkSignal() {
TelephonyManager mTelephonyManager = (TelephonyManager) NetworkSignalDetectionActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephonyManager != null) {
mTelephonyManager.listen(new PhoneStateListener() {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
new Thread(){
@Override
public void run() {
super.run();
while (true){
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
asu = signalStrength.getGsmSignalStrength();
lastSignal = -113 + 2 * asu;
if (lastSignal > 0) {
mobileNetworkSignal = lastSignal + "dBm";
}
// 步骤3:创建所需的消息对象
Message msg = Message.obtain();
msg.what = 1; // 消息标识
// 步骤4:在工作线程中 通过Handler发送消息到消息队列中
handler.sendMessage(msg);
}
}
}.start();
}
}, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
}
2.完整代码
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Button;
import com.ami.myzonghe_demo.R;
import com.ami.myzonghe_demo.mvp.base.BaseActivity22;
import com.blankj.utilcode.util.PhoneUtils;
import org.jetbrains.annotations.NotNull;
import butterknife.BindView;
public class NetworkSignalDetectionActivity extends BaseActivity22 {
@BindView(R.id.btnwifi)
Button btnwifi;
public Handler handler=new Handler(){
@Override
public void handleMessage(@NonNull @NotNull Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1:
// 获取系统wifi服务
WifiManager wm = (WifiManager) NetworkSignalDetectionActivity.this.getApplicationContext().getSystemService(WIFI_SERVICE);
// 获取当前所连接wifi的信息
WifiInfo wi = wm.getConnectionInfo();
int rssi = wi.getRssi();
Log.e("aaa","Wifi网络数据:"+rssi);
Log.e("aaa" , "数据强度:"+ lastSignal + " dBm");
Log.e("aaa" , "数据强度asu:"+ asu + " asu");
break;
}
}
};
private boolean is_wifi_speed;
private String mobileNetworkSignal;
private int lastSignal;
private int asu;
@Override
protected int Layout() {
return R.layout.activity_network_signal_detection;
}
@Override
protected void initView(Bundle savedInstanceState) {
//获取WiFi信号
is_wifi_speed =true;
// 采用继承Thread类实现多线程演示
new Thread() {
@Override
public void run() {
while (is_wifi_speed){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 步骤3:创建所需的消息对象
Message msg = Message.obtain();
msg.what = 1;
handler.sendMessage(msg);
}
}
}.start();
getMobileNetworkSignal();
}
@Override
protected void startCoding() {
}
/**
* 判断是否包含SIM卡
*
* @return 状态
*/
public static boolean hasSimCard(Context context) {
TelephonyManager telMgr = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
int simState = telMgr.getSimState();
boolean result = true;
switch (simState) {
case TelephonyManager.SIM_STATE_ABSENT:
case TelephonyManager.SIM_STATE_UNKNOWN:
result = false; // 没有SIM卡
break;
}
return result;
}
private void getMobileNetworkSignal() {
TelephonyManager mTelephonyManager = (TelephonyManager) NetworkSignalDetectionActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephonyManager != null) {
mTelephonyManager.listen(new PhoneStateListener() {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
new Thread(){
@Override
public void run() {
super.run();
while (true){
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
asu = signalStrength.getGsmSignalStrength();
lastSignal = -113 + 2 * asu;
if (lastSignal > 0) {
mobileNetworkSignal = lastSignal + "dBm";
}
// 步骤3:创建所需的消息对象
Message msg = Message.obtain();
msg.what = 1; // 消息标识
// 步骤4:在工作线程中 通过Handler发送消息到消息队列中
handler.sendMessage(msg);
}
}
}.start();
}
}, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
}
@Override
public void onSuccess(Object o) {
}
@Override
public void onError(String error) {
}
}
清单文件中需要这些权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>