public class DeviceUtil { /** * 是否有网 * @param context * @return */ 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; } /** * 返回版本名字 * 对应build.gradle中的versionName * @param context * @return */ public static String getVersionName(Context context) { String versionName = ""; try { PackageManager packageManager = context.getPackageManager(); PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0); versionName = packInfo.versionName; } catch (Exception e) { e.printStackTrace(); } return versionName; } /** * 返回版本号 * 对应build.gradle中的versionCode * @param context * @return */ public static String getVersionCode(Context context) { String versionCode = ""; try { PackageManager packageManager = context.getPackageManager(); PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0); versionCode = String.valueOf(packInfo.versionCode); } catch (Exception e) { e.printStackTrace(); } return versionCode; } /** * 获取手机品牌 * * @return */ public static String getPhoneBrand() { return android.os.Build.BRAND; } /** * 获取手机型号 * * @return */ public static String getPhoneModel() { return android.os.Build.MODEL; } /** * 获取手机Android 版本(4.4、5.0、5.1 ...) * @return */ public static String getBuildVersion() { return android.os.Build.VERSION.RELEASE; } /** * 获取AndroidManifest.xml里 的值 * @param context * @param name * @return */ public static String getMetaData(Context context, String name) { String value = null; try { ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); value = appInfo.metaData.getString(name); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return value; } public static String getAndroidId(Context context) { String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); return androidId; } public static String getAndroidSerial() { String serialNum = Build.SERIAL; return serialNum; } public static String getAndroidInfo(Context context) { return String.format("品牌:%s,型号:%s,版本:%s,Id:%s,序列:%s,Mac:%s",getPhoneBrand(), getPhoneModel(), getBuildVersion(), getAndroidId(context), getAndroidSerial(), DeviceMacUtil.getMacFromHardware(context)); } }