获取Android设备信息

    xiaoxiao2021-04-19  83

    应该不是很全,以后有时间再补充

    import android.app.ActivityManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Build; import android.os.Environment; import android.os.StatFs; import android.telephony.TelephonyManager; import android.text.format.Formatter; import android.view.WindowManager; import com.bh.resident.demo.MyApplication1; import com.bh.resident.demo.utils.SLog; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Enumeration; /** * 读取基本信息 * Created by liyin on 2017-02-13. */ public class ReadDeviceInfoFunction { TelephonyManager teleohonyManager; Context context; BatteryReceiver receiver; @Override public void init(Context context) { this.context = context; teleohonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); receiver = new BatteryReceiver(); } // 获取设备信息 public void getDeviceInfo() { SLog.Console("系统参数------------------------------begin"); //主板 String board = Build.BOARD; SLog.Console("主板:" + board); //系统定制商 String brand = Build.BRAND; SLog.Console("系统定制商:" + brand); //设备信息 String device = Build.DEVICE; SLog.Console("系统定制商:" + device); //显示屏参数 String display = Build.DISPLAY; SLog.Console("显示屏参数:" + display); //唯一编号 String fingderprint = Build.FINGERPRINT; SLog.Console("唯一编号:" + fingderprint); //硬件序列号 String serial = Build.SERIAL; SLog.Console("硬件序列号:" + serial); //修订版本列表 String id = Build.ID; SLog.Console("修订版本列表:" + id); //硬件制造商 String manufacturer = Build.MANUFACTURER; SLog.Console("硬件制造商:" + manufacturer); //版本 String model = Build.MODEL; SLog.Console("版本:" + model); //硬件名 String hardware = Build.HARDWARE; SLog.Console("硬件名:" + hardware); //手机产品名 String product = Build.PRODUCT; SLog.Console("手机产品名:" + product); //描述build的标签 String tags = Build.TAGS; SLog.Console("描述build的标签:" + tags); //Builder类型 String type = Build.TYPE; SLog.Console("Builder类型:" + type); //当前开发代号 String vcodename = Build.VERSION.CODENAME; SLog.Console("当前开发代号:" + vcodename); //源码控制版本号 String vincremental = Build.VERSION.INCREMENTAL; SLog.Console("源码控制版本号:" + vincremental); //版本字符串 String vrelease = Build.VERSION.RELEASE; SLog.Console("版本字符串:" + vrelease); //版本号 int vsdkint = Build.VERSION.SDK_INT; SLog.Console("版本号:" + vsdkint); //HOST值 String host = Build.HOST; SLog.Console("HOST值:" + host); //User名 String user = Build.USER; SLog.Console("User名:" + user); //编译时间 long time = Build.TIME; SLog.Console("编译时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date(time))); //OS版本号 String osVersion = System.getProperty("os.version"); SLog.Console("OS版本号:" + osVersion); // OS名称 String osName = System.getProperty("os.name"); SLog.Console("OS名称:" + osName); //OS架构 String osArch = System.getProperty("os.arch"); SLog.Console("OS架构:" + osArch); //home属性 String osUserHome = System.getProperty("os.home"); SLog.Console("home属性:" + osUserHome); //name属性 String osUserName = System.getProperty("os.name"); SLog.Console("name属性 :" + osUserName); //dir属性 String osUserDir = System.getProperty("os.dir"); SLog.Console("dir属性:" + osUserDir); //时区 String osUserTimeZone = System.getProperty("os.timezone"); SLog.Console("时区:" + osUserTimeZone); //电话号 String phoneNum = teleohonyManager.getLine1Number(); SLog.Console("手机号:" + phoneNum); //集成电路卡标识 String iccid = teleohonyManager.getSimSerialNumber(); SLog.Console("集成电路卡标识:" + iccid); //手机电量 IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); MyApplication1.getInstance().registerReceiver(receiver, filter);//注册BroadcastReceiver //设备id String mDeviceId = teleohonyManager.getDeviceId(); SLog.Console("设备id:" + mDeviceId); getPhoneType(); getLocalMacAddress(); getLocalIpAddress(); getCpuInfo(); getTotalMemory(); getAvailMemory(); getWeithAndHeight(); //TODO 连接wifi名字 } /** * 获取当前手机支持的移动网络类型 */ private void getPhoneType() { String phoneType = ""; switch (teleohonyManager.getPhoneType()) { case TelephonyManager.PHONE_TYPE_NONE: phoneType = "NONE: "; break; case TelephonyManager.PHONE_TYPE_GSM: phoneType = "GSM: IMEI"; break; case TelephonyManager.PHONE_TYPE_CDMA: phoneType = "CDMA: MEID/ESN"; break; default: phoneType = "UNKNOWN: ID"; break; } SLog.Console("手机网络类型:" + phoneType); } /** * 获取mac地址 * 只有手机开启wifi才能获取到mac地址 */ public void getLocalMacAddress() { WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); SLog.Console("MAC地址:" + info.getMacAddress()); } /** * 获取IP地址 */ public void getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements(); ) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf .getInetAddresses(); enumIpAddr.hasMoreElements(); ) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { SLog.Console("IP地址:" + inetAddress.getHostAddress().toString()); } } } } catch (SocketException ex) { } } /** * 手机CPU信息 */ private void getCpuInfo() { String str1 = "/proc/cpuinfo"; String str2 = ""; String[] cpuInfo = {"", ""}; //1-cpu型号 //2-cpu频率 String[] arrayOfString; try { FileReader fr = new FileReader(str1); BufferedReader localBufferedReader = new BufferedReader(fr, 8192); str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); for (int i = 2; i < arrayOfString.length; i++) { cpuInfo[0] = cpuInfo[0] + arrayOfString[i] + " "; } str2 = localBufferedReader.readLine(); arrayOfString = str2.split("\\s+"); cpuInfo[1] += arrayOfString[2]; localBufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } SLog.Console("cpu型号:" + cpuInfo[0] + "cpu频率:" + cpuInfo[1]); } /** * 获取当前可用内存大小 */ private void getAvailMemory() { ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo(); am.getMemoryInfo(mi); //mi.availMem; 当前系统的可用内存 SLog.Console("可用运行内存大小:" + Formatter.formatFileSize(context, mi.availMem)); } /** * 获得系统总内存 */ private void getTotalMemory() { File path = Environment.getDataDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long totalBlocks = stat.getBlockCount(); SLog.Console("总内存:" + Formatter.formatFileSize(context, blockSize * totalBlocks)); } /** * 获取屏幕宽高 */ private void getWeithAndHeight() { WindowManager mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); int width = mWindowManager.getDefaultDisplay().getWidth(); int height = mWindowManager.getDefaultDisplay().getHeight(); SLog.Console("屏幕尺寸:" + width + "x" + height); } private class BatteryReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) { int current = intent.getExtras().getInt("level");//获得当前电量 int total = intent.getExtras().getInt("scale");//获得总电量 int percent = current * 100 / total; SLog.Console("电量:" + percent + "%。"); context.unregisterReceiver(receiver); } } } }

    欢迎 补充 修正

    转载请注明原文地址: https://ju.6miu.com/read-675792.html

    最新回复(0)