目标信息:
1、手机品牌
2、手机型号
3、屏幕分辨率
4、像素密度
5、屏幕尺寸
6、Android 系统版本
7、运行内存
8、储存空间
9、网络连接是否成功
10、是否支持WiFi
11、数据网络是否可用
12、正在使用的网路(WiFi / 数据网)
13、当前网络(WiFi / 3G / 4G)
14、电池电量
15、电池电压
16、电池温度
17、电池状态( 充电状态 / 放电状态 / 未充电 / 充满电)
18、电池报警(电池电压过高 / 电池过热 /)
获取结果
代码介绍
代码如下:
【在 AndroidManifest.xml 中声明权限】
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
【MainActivity.java】代码
package com.example.myapplication; import android.annotation.SuppressLint; import android.app.ActivityManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.BatteryManager; import android.os.Environment; import android.os.StatFs; import android.os.Bundle; import android.telephony.TelephonyManager; import android.text.format.Formatter; import android.util.DisplayMetrics; import android.util.Log; import android.view.WindowManager; import android.widget.TextView; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.text.DecimalFormat; import static java.lang.Math.sqrt; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); //获取电量 setContentView(R.layout.activity_main); initView(); } @SuppressLint("SetTextI18n") private void initView() { TextView Basic01 = findViewById(R.id.Basic01); TextView Basic02 = findViewById(R.id.Basic02); TextView Basic03 = findViewById(R.id.Basic03); TextView Basic04 = findViewById(R.id.Basic04); TextView Basic05 = findViewById(R.id.Basic05); TextView Basic06 = findViewById(R.id.Basic06); Basic01.setText("手机品牌:" + android.os.Build.BRAND); Basic02.setText("手机型号:" + android.os.Build.MODEL); Basic03.setText("屏分辨率:" + getScreenHeight(MainActivity.this) + "x" + getScreenWidth(MainActivity.this) + " px"); Basic04.setText("像素密度:" + getScreenDensity(MainActivity.this) + " dpi"); Basic05.setText("屏幕尺寸:" + getMeasure(getScreenHeight(MainActivity.this), getScreenWidth(MainActivity.this), (int) getScreenDensity(MainActivity.this)) + "英寸"); Basic06.setText("系统版本: Android " + android.os.Build.VERSION.RELEASE); TextView Memory01 = findViewById(R.id.Memory01); TextView Memory02 = findViewById(R.id.Memory02); Memory01.setText("运行内存:" + getAvailMemory(MainActivity.this) + " / " + getTotalMemory(MainActivity.this) + "【可用/总】"); Memory02.setText("储存空间:" + getAvailableInternalMemorySize(MainActivity.this) + " / " + getInternalMemorySize(MainActivity.this) + "【可用/总】"); } /** 获取当前屏幕的高度 */ public static int getScreenHeight(Context context) { //获取系统服务 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(dm); //获取显示参数保存到dm中 return dm.heightPixels; //返回屏幕的宽度 } /** 获取当前屏幕的宽度 */ public static int getScreenWidth(Context context) { //获取系统服务 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(dm); //获取显示参数保存到dm中 return dm.widthPixels; //返回屏幕的宽度 } /** 获取当前屏幕的像素密度 */ //像素密度density,表示一个dp包含多少个Px单位,和标准dpi的比例(160px/inc) public static float getScreenDensity(Context context) { //获取系统服务 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(dm); //获取显示参数保存到dm中 return dm.density * 160; //返回屏幕的宽度 } /** 计算屏幕尺寸 */ public static String getMeasure(int height, int width, int dpi) { float x = (float) sqrt(((height * height) + (width * width)) / (dpi * dpi)); DecimalFormat decimalFormat = new DecimalFormat(".00");//构造方法的字符格式这里如果小数不足2位,会以0补足. String str = decimalFormat.format(x); //format 返回的是字符串 return str; //返回屏幕的宽度 } /** 获取当前可用运行内存大小 */ public static String getAvailMemory(Context context) { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); am.getMemoryInfo(mi); return Formatter.formatFileSize(context, mi.availMem);// 将获取的内存大小规格化 } /** 获取android总运行内存大小 */ public static String getTotalMemory(Context context) { String str1 = "/proc/meminfo";// 系统内存信息文件 String str2; String[] arrayOfString; long initial_memory = 0; try { FileReader localFileReader = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192); str2 = localBufferedReader.readLine();// 读取meminfo第一行,系统总内存大小 arrayOfString = str2.split("//s+"); for (String num : arrayOfString) { Log.i(str2, num + "/t"); } int i = Integer.valueOf(arrayOfString[1]);// 获得系统总内存,单位是KB initial_memory = (long) i * 1024;//int值乘以1024转换为long类型 localBufferedReader.close(); } catch (IOException ignored) { } return Formatter.formatFileSize(context, initial_memory);// Byte转换为KB或者MB,内存大小规格化 } /** 获取手机内部存储空间,以M,G为单位的容量 */ public static String getInternalMemorySize(Context context) { File file = Environment.getDataDirectory(); StatFs statFs = new StatFs(file.getPath()); long blockSizeLong = statFs.getBlockSizeLong(); long blockCountLong = statFs.getBlockCountLong(); long size = blockCountLong * blockSizeLong; return Formatter.formatFileSize(context, size); } /** 获取手机内部可用存储空间,以M,G为单位的容量 */ public static String getAvailableInternalMemorySize(Context context) { File file = Environment.getDataDirectory(); StatFs statFs = new StatFs(file.getPath()); long availableBlocksLong = statFs.getAvailableBlocksLong(); long blockSizeLong = statFs.getBlockSizeLong(); return Formatter.formatFileSize(context, availableBlocksLong * blockSizeLong); } /** 是否有网络连接 */ public static boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } } return false; } /** WiFi是否可用 */ public static boolean isWifiConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mWiFiNetworkInfo != null) { return mWiFiNetworkInfo.isAvailable(); } } return false; } /** 数据流量是否可用 */ public static boolean isMobileConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mMobileNetworkInfo != null) { return mMobileNetworkInfo.isAvailable(); } } return false; } /** 获取当前网络连接的类型信息 */ public static int getConnectedType(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { return mNetworkInfo.getType(); } } return -1; } /** 获取当前的网络状态 */ public static int getAPNType(Context context) { int netType = 0; ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo == null) { return netType; } int nType = networkInfo.getType(); if (nType == ConnectivityManager.TYPE_WIFI) { netType = 1; // wifi } else if (nType == ConnectivityManager.TYPE_MOBILE) { int nSubType = networkInfo.getSubtype(); TelephonyManager mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS && !mTelephony.isNetworkRoaming()) { netType = 2;// 3G } else { netType = 3;// 4G } } return netType; } private BroadcastReceiver batteryReceiver = new BroadcastReceiver(){ @Override public void onReceive(Context context, Intent intent) { getCell(intent); //获取手机电池信息 Network(); //获取网络信息 } }; /** 获取手机电池信息 */ @SuppressLint("SetTextI18n") private void getCell(Intent intent) { int mLevel = intent.getIntExtra("level", 0); //电池电量 int BatteryV = intent.getIntExtra("voltage", 0); //电池电压 int BatteryT = intent.getIntExtra("temperature", 0); //电池温度 TextView Cell01 = findViewById(R.id.Cell01); TextView Cell02 = findViewById(R.id.Cell02); TextView Cell03 = findViewById(R.id.Cell03); TextView Cell04 = findViewById(R.id.Cell04); TextView Cell05 = findViewById(R.id.Cell05); Cell01.setText("电池电量:" + mLevel + "%"); Cell02.setText("电池电压:" + (BatteryV * 0.001) + "V"); Cell03.setText("电池温度:" + (BatteryT * 0.001) + "℃"); switch (intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN)) { case BatteryManager.BATTERY_STATUS_CHARGING: Cell04.setText("电池状态:充电状态");break; case BatteryManager.BATTERY_STATUS_DISCHARGING: Cell04.setText("电池状态:放电状态");break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING: Cell04.setText("电池状态:未充电");break; case BatteryManager.BATTERY_STATUS_FULL: Cell04.setText("电池状态:充满电");break; case BatteryManager.BATTERY_STATUS_UNKNOWN: Cell04.setText("电池状态:未知道状态");break; } switch (intent.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN)) { case BatteryManager.BATTERY_HEALTH_UNKNOWN: Cell05.setText("电池报警:未知错误");break; case BatteryManager.BATTERY_HEALTH_GOOD: Cell05.setText("电池报警:电池状态良好");break; case BatteryManager.BATTERY_HEALTH_DEAD: Cell05.setText("电池报警:电池没有电");break; case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: Cell05.setText("电池报警:电池电压过高");break; case BatteryManager.BATTERY_HEALTH_OVERHEAT: Cell05.setText("电池报警:电池过热");break; } } /** 获取网络信息 */ @SuppressLint("SetTextI18n") private void Network() { TextView Network01 = findViewById(R.id.Network01); TextView Network02 = findViewById(R.id.Network02); TextView Network03 = findViewById(R.id.Network03); TextView Network04 = findViewById(R.id.Network04); TextView Network05 = findViewById(R.id.Network05); if(isNetworkConnected(MainActivity.this)){ Network01.setText("网路连接:网络连接成功"); }else{ Network01.setText("网路连接:网络连接失败"); } if(isWifiConnected(MainActivity.this)){ Network02.setText("Wifi 网络:支持"); }else{ Network02.setText("Wifi 网络:不支持"); } if(isMobileConnected(MainActivity.this)){ Network03.setText("数据网络:可用"); }else{ Network03.setText("数据网络:不可用"); } switch (getConnectedType(MainActivity.this)){ case -1: Network04.setText("网络类型:无网络"); break; case 0: Network04.setText("网络类型:正在使用数据流量上网"); break; case 1: Network04.setText("网络类型:正在使用 Wifi上网"); break; default: Network04.setText("网络类型:未知网络"); break; } //没有网络0:WIFI网络1:3G网络2:2G网络3 switch (getAPNType(MainActivity.this)){ case 0: Network05.setText("网络状态:无网络"); break; case 1: Network05.setText("网络状态:WIFI网络"); break; case 2: Network05.setText("网络状态:3G网络"); break; case 3: Network05.setText("网络状态:4G网络"); break; default: Network05.setText("网络状态:其他网络"); break; } } }
【布局文件:activity_main.xml】
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="25dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:text="【当前手机信息】" android:textSize="20sp" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="10dp" android:layout_marginRight="5dp" android:text="基本信息" android:textSize="12sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="@drawable/bg01" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/Basic01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="手机型号" android:textSize="16sp" /> <TextView android:id="@+id/Basic02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="屏幕高度" android:textSize="16sp" /> <TextView android:id="@+id/Basic03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="屏幕宽度" android:textSize="16sp" /> <TextView android:id="@+id/Basic04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="像素密度" android:textSize="16sp" /> <TextView android:id="@+id/Basic05" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="屏幕尺寸" android:textSize="16sp" /> <TextView android:id="@+id/Basic06" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="屏幕尺寸" android:textSize="16sp" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="10dp" android:layout_marginRight="5dp" android:text="内存信息" android:textSize="12sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="@drawable/bg01" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/Memory01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="内存信息" android:textSize="16sp" /> <TextView android:id="@+id/Memory02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="内存信息" android:textSize="16sp" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="10dp" android:layout_marginRight="5dp" android:text="网络信息" android:textSize="12sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:background="@drawable/bg01" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/Network01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="网络信息" android:textSize="16sp" /> <TextView android:id="@+id/Network02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="网络信息" android:textSize="16sp" /> <TextView android:id="@+id/Network03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="网络信息" android:textSize="16sp" /> <TextView android:id="@+id/Network04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="网络信息" android:textSize="16sp" /> <TextView android:id="@+id/Network05" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="网络信息" android:textSize="16sp" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="10dp" android:layout_marginRight="5dp" android:text="电池信息" android:textSize="12sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_marginBottom="150dp" android:background="@drawable/bg01" android:orientation="vertical" android:padding="10dp"> <TextView android:id="@+id/Cell01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电池信息" android:textSize="16sp" /> <TextView android:id="@+id/Cell02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="电池信息" android:textSize="16sp" /> <TextView android:id="@+id/Cell03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="电池信息" android:textSize="16sp" /> <TextView android:id="@+id/Cell04" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="电池信息" android:textSize="16sp" /> <TextView android:id="@+id/Cell05" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="电池信息" android:textSize="16sp" /> </LinearLayout> </LinearLayout> </ScrollView> </LinearLayout>
【背景边框:bg01.xml】
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ffffff"/> <stroke android:width="1dip" android:color="#44c7ff" /> <corners android:radius="5dp"/> </shape>
原创文章,作者:Carrie001128,如若转载,请注明出处:https://blog.ytso.com/272931.html