Android获取设备各项信息(设备id、ip地址、设备名称、运行商、品牌、型号、分辨率、处理器、国家码、系统语言、网络类型、oaid、android版本、操作系统版本、mac地址、应用程序签名..)1:https://developer.aliyun.com/article/1473468
获取设备识别码
方案一
// build.gradle需要引入获取DeviceIdentifier类库 compileOnly 'com.github.gzu-liyujiang:Android_CN_OAID:4.2.3' • 1 • 2
public static String getImei() { if (Build.VERSION.SDK_INT < 29) { return DeviceIdentifier.getOAID(context)); } else { return DeviceIdentifier.getIMEI(context)); } }
方案二
public static String getImei() { TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (tm == null) { return ""; } return tm.getDeviceId(); }
获取mac地址
public static String getMac() { if (context == null) { return ""; } WifiManager wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); WifiInfo wi = wm.getConnectionInfo(); if (wi == null || wi.getMacAddress() == null) { return ""; } return wi.getMacAddress().trim(); }
获取应用包名
public static String getPackageName() { if (context == null) { return ""; } return context.getPackageName(); }
获取应用签名
public static String getAppSignature() { if (context == null) { return ""; } byte[] cert = new byte[0]; try { PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES); cert = packageInfo.signatures[0].toByteArray(); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return cert.toString(); }
获取app版本
public stpublic static String getAppVersion() { if (context == null) { return ""; } PackageManager pm = context.getPackageManager(); PackageInfo pi = null; try { pi = pm.getPackageInfo(context.getPackageName(), 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return pi.versionName; }
获取版本号
public static int getVersionCode() { if (context == null) { return 0; } PackageManager pm = context.getPackageManager(); PackageInfo pi = null; try { pi = pm.getPackageInfo(context.getPackageName(), 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return pi.versionCode; }
获取分辨率
public static String getResolution() { if (context == null) { return ""; } DisplayMetrics dm = context.getResources().getDisplayMetrics(); int widthPixels = dm.widthPixels; int heightPixels = dm.heightPixels; return widthPixels + "*" + heightPixels; }
获取国家码
public static String getCountry() { TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (tm == null) { return ""; } return tm.getNetworkCountryIso(); }
获取系统语言
public static String getLanguage() { return Locale.getDefault().getLanguage(); }
获取设备网络代码
public static String getMccmnc() { TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (tm == null) { return ""; } return tm.getNetworkOperator(); }
获取设备网络类型
public static String getNetworkType() { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (cm == null) { return "Unknown"; } NetworkInfo activeNetInfo = cm.getActiveNetworkInfo(); if (activeNetInfo == null || !activeNetInfo.isAvailable()) { return "Unknown"; } NetworkInfo wifiInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (wifiInfo != null) { NetworkInfo.State state = wifiInfo.getState(); if (state != null && state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) { return "WiFi"; } } NetworkInfo networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (networkInfo != null) { NetworkInfo.State state = networkInfo.getState(); String subtypeName = networkInfo.getSubtypeName(); if (state != null && state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) { switch (activeNetInfo.getSubtype()) { case TelephonyManager.NETWORK_TYPE_GPRS: case TelephonyManager.NETWORK_TYPE_CDMA: case TelephonyManager.NETWORK_TYPE_EDGE: case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_IDEN: return "2G"; case TelephonyManager.NETWORK_TYPE_EVDO_A: case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_EVDO_B: case TelephonyManager.NETWORK_TYPE_EHRPD: case TelephonyManager.NETWORK_TYPE_HSPAP: return "3G"; case TelephonyManager.NETWORK_TYPE_LTE: return "4G"; default: if (subtypeName.equalsIgnoreCase("TD-SCDMA") || subtypeName.equalsIgnoreCase("WCDMA") || subtypeName.equalsIgnoreCase("CDMA2000")) { return "3G"; } return "Unknown"; } } } return "Unknown"; }
获取android版本
public static int getAndroidVersion() { return Build.VERSION.SDK_INT; } public
获取操作系统版本
public static String getOsVersion() { return Build.VERSION.RELEASE; }
获取目标sdk版本号
public static int getTargetSdkVer() { if (context == null) { return 0; } return context.getApplicationInfo().targetSdkVersion; }
获取应用第一次安装时间
public static long getFirstInstallTime() { if (context == null) { return 0; } PackageManager pm = context.getPackageManager(); PackageInfo packageInfo = null; try { packageInfo = pm.getPackageInfo(context.getPackageName(), 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return packageInfo.firstInstallTime; }