android 裝置資訊工具類
阿新 • • 發佈:2019-02-15
import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.SystemClock;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;
import android.util.DisplayMetrics;
public class DeviceUtils {
private static final String TAG = DeviceUtils.class.getSimpleName();
private static final String CMCC_ISP = "46000";//中國移動
private static final String CMCC2_ISP = "46002";//中國移動
private static final String CU_ISP = "46001";//中國聯通
private static final String CT_ISP = "46003";//中國電信
/**
* 獲取裝置的系統版本號
*/
public static int getDeviceSDK() {
int sdk = Build.VERSION.SDK_INT;
return sdk;
}
/**
* 獲取裝置的型號
*/
public static String getDeviceName() {
String model = Build.MODEL;
return model;
}
public static String getIMSI(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String IMSI = telephonyManager.getSubscriberId();
return IMSI;
}
public static String getIMEI(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String IMEI = telephonyManager.getDeviceId();
return IMEI;
}
/**
* 獲取手機網路運營商型別
*
* @param context
* @return
*/
public static String getPhoneISP(Context context) {
if (context == null) {
return "";
}
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String teleCompany = "";
String np = manager.getNetworkOperator();
if (np != null) {
if (np.equals(CMCC_ISP) || np.equals(CMCC2_ISP)) {
teleCompany = "中國移動";
} else if (np.startsWith(CU_ISP)) {
teleCompany = "中國聯通";
} else if (np.startsWith(CT_ISP)) {
teleCompany = "中國電信";
}
}
return teleCompany;
}
/**
* 獲取螢幕資訊
*
* @param context
* @return
*/
public static DisplayMetrics getDisplayMetrics(Context context) {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
return dm;
}
/**
* 獲取/列印螢幕資訊
*
* @param context
* @return
*/
public static DisplayMetrics printDisplayInfo(Context context) {
DisplayMetrics dm = getDisplayMetrics(context);
StringBuilder sb = new StringBuilder();
sb.append("\ndensity :").append(dm.density);
sb.append("\ndensityDpi :").append(dm.densityDpi);
sb.append("\nheightPixels :").append(dm.heightPixels);
sb.append("\nwidthPixels :").append(dm.widthPixels);
sb.append("\nscaledDensity :").append(dm.scaledDensity);
sb.append("\nxdpi :").append(dm.xdpi);
sb.append("\nydpi :").append(dm.ydpi);
LogUtils.i(TAG, sb.toString());
return dm;
}
/**
* 獲取系統當前可用記憶體大小
*
* @param context
* @return
*/
@TargetApi(Build.VERSION_CODES.CUPCAKE)
public static String getAvailMemory(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
am.getMemoryInfo(mi);
return Formatter.formatFileSize(context, mi.availMem);// 將獲取的記憶體大小規格化
}
/**
* 獲取 MAC 地址
* 須配置android.permission.ACCESS_WIFI_STATE許可權
*/
public static String getMacAddress(Context context) {
//wifi mac地址
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String mac = info.getMacAddress();
LogUtils.i(TAG, " MAC:" + mac);
return mac;
}
/**
* 獲取 開機時間
*/
public static String getBootTimeString() {
long ut = SystemClock.elapsedRealtime() / 1000;
int h = (int) ((ut / 3600));
int m = (int) ((ut / 60) % 60);
LogUtils.i(TAG, h + ":" + m);
return h + ":" + m;
}
}