赞
踩
- public static String getDeviceId() {
- if (context == null) {
- return "";
- }
- return DeviceIdentifier.getAndroidID(context);
- }
- public static String getIp() {
- String networkType = getNetworkType();
- if (networkType.equals("WiFi")) {
- WifiManager wifiManager = null;
- try {
- wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
- WifiInfo wifiInfo = wifiManager.getConnectionInfo();
- int i = wifiInfo.getIpAddress();
- return intToIp(i);
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- } else if (networkType.equals("2G") || networkType.equals("3G") || networkType.equals("4G")) {
- 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()) {
- return inetAddress.getHostAddress().toString();
- }
- }
- }
- } catch (Exception ex) {
- return null;
- }
- } else {
- return null;
- }
- return null;
- }
-
- /**
- * 将ip的整数形式转换成ip形式
- */
- public static String intToIp(int i) {
- return (i & 0xFF) + "." +
- ((i >> 8) & 0xFF) + "." +
- ((i >> 16) & 0xFF) + "." +
- (i >> 24 & 0xFF);
- }
- public static String getDeviceName() {
- return Build.DEVICE;
- }
- public static String getDeviceModel() {
- return Build.MODEL;
- }
- public static String getDeviceBoard() {
- return Build.BOARD;
- }
- public static String getDeviceBrand() {
- return Build.BRAND;
- }
- public static String getDeviceManufacturer() {
- return Build.MANUFACTURER;
- }
- public static String getOAID() {
- if (context == null) {
- return "";
- }
- if (DeviceID.supportedOAID(context)) {
- return DeviceID.getOAID();
- }
- return "";
- }
- public static String getImei() {
- TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
- if (tm == null) {
- return "";
- }
- return tm.getDeviceId();
- }
- 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();
- }
- public 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";
- }
- public static int getAndroidVersion() {
- return Build.VERSION.SDK_INT;
- }
- public static String getOsVersion() {
- return Build.VERSION.RELEASE;
- }
- 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;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。