android获取版本信息、屏幕信息和设备编号
16lz
2021-01-23
1、获取版本信息:
/** * 获取version */try {PackageInfo info=getPackageManager().getPackageInfo(this.getPackageName(), 1);Log.i("info","info.versionCode:"+info.versionCode);Log.i("info","info.versionName:"+info.versionName);} catch (NameNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}
2、获取屏幕信息
/** * 获取屏幕信息 */DisplayMetrics dm=getResources().getDisplayMetrics();Log.i("info","widthPixels:"+dm.widthPixels);Log.i("info","heightPixels:"+dm.heightPixels);
3、获取设备编号
public static String getDeviceId(Context context) {String deviceId = null;deviceId = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();if (deviceId == null && Build.VERSION.SDK_INT > 9) {deviceId = Secure.getString(context.getContentResolver(),Secure.ANDROID_ID);if (deviceId == null) {ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo networkInfo = cm.getActiveNetworkInfo();if (networkInfo != null&& networkInfo.isAvailable()&& networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {WifiManager wm = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);deviceId = wm.getConnectionInfo().getMacAddress();} else {deviceId = UUID.randomUUID().toString();}}}if (deviceId != null && deviceId.length() < 28) {int len = 28 - deviceId.length();for (int i = 0; i < len; i++) {deviceId = "0" + deviceId;}}return deviceId;}
3.1、基本上,所有设备通过((TelephonyManager) context.getSystemService(Context. TELEPHONY_SERVICE)).getDeviceId();获取一个设备编号。但是某些平板电脑会返回空。
3.2、通过 Secure.getString(s_instance.getContentResolver(), Secure.ANDROID_ID); 会返回一个android系统唯一区分的64-bit 哈希字符串。但是android2.2或者是某些山寨手机使用这个也是有问题的,它会返回一个固定的值 9774d56d682e549c。
3.3、如果用户链接上Wi-Fi,则可以获取用户的MacAddress。
3.4、如果前两个都没有获取到udid,那么就在程序启动的时候创建一个随机的uuid,然后保存起来。这个算是兼容方案,当然这样的设备并不会很多。
更多相关文章
- android 判断屏幕是横屏还是竖屏
- Android 三星128G SD卡格式化为内部存储设备,显示为256G
- Android 监听屏幕熄屏亮屏和主动唤醒屏幕
- android 获取屏幕的宽度和高度
- cryptfs-password-manager, Android设备加密密码管理器
- 判断android设备是否支持硬解码
- Android小程序之个人信息管理系统
- android屏幕休眠和唤醒两种方法(newWakeLock)