添加网络监控
This commit is contained in:
@@ -1,2 +1,5 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.mogo.utils" />
|
||||
package="com.mogo.utils" >
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
</manifest>
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.mogo.utils.network.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.telephony.PhoneStateListener;
|
||||
import android.telephony.SignalStrength;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.util.Log;
|
||||
|
||||
/**
|
||||
* @author liujing
|
||||
* @description 网络状态监听
|
||||
* @since: 7/29/21
|
||||
*/
|
||||
public class NetworkStatusUtil {
|
||||
|
||||
private static PhoneStatListener phoneStatListener;
|
||||
private static int mSignalStrength;
|
||||
|
||||
/**
|
||||
* 监听网络强度
|
||||
*/
|
||||
public static int networkState(Context context) {
|
||||
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
if (telephonyManager == null) {
|
||||
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
|
||||
}
|
||||
if (phoneStatListener == null) {
|
||||
phoneStatListener = new PhoneStatListener();
|
||||
}
|
||||
telephonyManager.listen(phoneStatListener, PhoneStatListener.LISTEN_SIGNAL_STRENGTHS);
|
||||
return mSignalStrength;
|
||||
}
|
||||
|
||||
/**
|
||||
* 监听网络信号的强度变化
|
||||
*/
|
||||
static class PhoneStatListener extends PhoneStateListener {
|
||||
@Override
|
||||
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
|
||||
//获取信号强度变化
|
||||
super.onSignalStrengthsChanged(signalStrength);
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
mSignalStrength = signalStrength.getLevel();
|
||||
return;
|
||||
}
|
||||
mSignalStrength = signalStrength.getGsmSignalStrength();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -3,10 +3,12 @@ package com.mogo.module.check.view;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
import android.widget.Button;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
@@ -16,6 +18,8 @@ import com.mogo.module.check.R;
|
||||
import com.mogo.module.check.model.CheckItemInfo;
|
||||
import com.mogo.module.common.view.SpacesItemDecoration;
|
||||
import com.mogo.utils.CommonUtils;
|
||||
import com.mogo.utils.NetworkUtils;
|
||||
import com.mogo.utils.network.utils.NetworkStatusUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -30,22 +34,27 @@ public class CheckActivity extends AppCompatActivity {
|
||||
private RecyclerView mRecyclerView;
|
||||
private ArrayList dataArrayList = new ArrayList();
|
||||
private CheckItemInfo mCheckItemInfo = new CheckItemInfo();
|
||||
private Context context = getApplication().getBaseContext();
|
||||
private Context context;
|
||||
private static int mSignalStrength;
|
||||
private Button mButton;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_check);
|
||||
initView();
|
||||
findViewById(R.id.btnBack).setOnClickListener(v -> {
|
||||
finish();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表View初始化
|
||||
*/
|
||||
public void initView() {
|
||||
mButton = findViewById(R.id.btnBack);
|
||||
context = mButton.getContext();
|
||||
mButton.setOnClickListener(v -> {
|
||||
finish();
|
||||
});
|
||||
|
||||
mRecyclerView = findViewById(R.id.check_list);
|
||||
mRecyclerView.setAdapter(new CheckAdapter(context, dataArrayList));
|
||||
CheckLinearLayout linearLayoutManager =
|
||||
@@ -54,6 +63,8 @@ public class CheckActivity extends AppCompatActivity {
|
||||
mRecyclerView.setLayoutManager(linearLayoutManager);
|
||||
|
||||
versionCheckResult();
|
||||
battery();
|
||||
netStatus();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -90,12 +101,18 @@ public class CheckActivity extends AppCompatActivity {
|
||||
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
|
||||
//电量百分比
|
||||
float batteryPct = level / (float) scale;
|
||||
Log.d(TAG, "电池百分比" + String.valueOf(batteryPct));
|
||||
}
|
||||
|
||||
/**
|
||||
* 网路
|
||||
* 网络
|
||||
*/
|
||||
public void netStatus(){
|
||||
public void netStatus() {
|
||||
//网络类型
|
||||
String networkType = CommonUtils.getNetworkType(context);
|
||||
//网络强度
|
||||
mSignalStrength = NetworkStatusUtil.networkState(context);
|
||||
Log.d(TAG, "网络类型:" + networkType + "网络强度:" + mSignalStrength);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user