前言:據(jù)項(xiàng)目需求,整理了一個(gè)AndroidUtil工具類,可獲取手機(jī)信息 & MAC地址 & 開機(jī)時(shí)間 & ip地址 &手機(jī)的imei、imsi等號(hào)碼。
/**
* @desc 手機(jī)信息 & MAC地址 & 開機(jī)時(shí)間 & ip地址 &手機(jī)的imei叫乌、imsi等號(hào)碼
* @auth 方毅超
* @time 2017/10/24 11:27
*/
public class AndroidUtil {
/**
* MAC地址
*
* @return
*/
public static String getMacAddress() {
/*獲取mac地址有一點(diǎn)需要注意的就是android 6.0版本后,以下注釋方法不再適用徽缚,
不管任何手機(jī)都會(huì)返回"02:00:00:00:00:00"這個(gè)默認(rèn)的mac地址憨奸,
這是googel官方為了加強(qiáng)權(quán)限管理而禁用了getSYstemService(Context.WIFI_SERVICE)方法來獲得mac地址。*/
// String macAddress= "";
// WifiManager wifiManager = (WifiManager) MyApp.getContext().getSystemService(Context.WIFI_SERVICE);
// WifiInfo wifiInfo = wifiManager.getConnectionInfo();
// macAddress = wifiInfo.getMacAddress();
// return macAddress;
String macAddress = null;
StringBuffer buf = new StringBuffer();
NetworkInterface networkInterface = null;
try {
networkInterface = NetworkInterface.getByName("eth1");
if (networkInterface == null) {
networkInterface = NetworkInterface.getByName("wlan0");
}
if (networkInterface == null) {
return "02:00:00:00:00:02";
}
byte[] addr = networkInterface.getHardwareAddress();
for (byte b : addr) {
buf.append(String.format("%02X:", b));
}
if (buf.length() > 0) {
buf.deleteCharAt(buf.length() - 1);
}
macAddress = buf.toString();
} catch (SocketException e) {
e.printStackTrace();
return "02:00:00:00:00:02";
}
return macAddress;
}
/**
* 獲得IP地址凿试,分為兩種情況排宰,一是wifi下,二是移動(dòng)網(wǎng)絡(luò)下红省,得到的ip地址是不一樣的
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
*/
public static String getIPAddress(Context context) {
NetworkInfo info = ((ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (info != null && info.isConnected()) {
if (info.getType() == ConnectivityManager.TYPE_MOBILE) {//當(dāng)前使用2G/3G/4G網(wǎng)絡(luò)
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() && inetAddress instanceof Inet4Address) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
} else if (info.getType() == ConnectivityManager.TYPE_WIFI) {//當(dāng)前使用無線網(wǎng)絡(luò)
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
//調(diào)用方法將int轉(zhuǎn)換為地址字符串
String ipAddress = intIP2StringIP(wifiInfo.getIpAddress());//得到IPV4地址
return ipAddress;
}
} else {
//當(dāng)前無網(wǎng)絡(luò)連接,請(qǐng)?jiān)谠O(shè)置中打開網(wǎng)絡(luò)
}
return null;
}
/**
* 將得到的int類型的IP轉(zhuǎn)換為String類型
*
* @param ip
* @return
*/
private static String intIP2StringIP(int ip) {
return (ip & 0xFF) + "." +
((ip >> 8) & 0xFF) + "." +
((ip >> 16) & 0xFF) + "." +
(ip >> 24 & 0xFF);
}
/**
* 獲取 ANDROID_ID
*/
public static String getAndroidId(Context context) {
String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
return androidId;
}
/**
* 獲取 開機(jī)時(shí)間
*/
public static String getBootTimeString() {
long ut = SystemClock.elapsedRealtime() / 1000;
int h = (int) ((ut / 3600));
int m = (int) ((ut / 60) % 60);
return h + ":" + m;
}
/**
* 手機(jī)信息
* 需要 <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
*
* @return
*/
public static String printSystemInfo() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = dateFormat.format(date);
StringBuilder sb = new StringBuilder();
sb.append("_______ 系統(tǒng)信息 ").append(time).append(" ______________");
sb.append("\nID :").append(Build.ID); // Either a changelist number, or a label like "M4-rc20".
sb.append("\nBRAND :").append(Build.BRAND); //品牌名 如 Xiaomi
sb.append("\nMODEL :").append(Build.MODEL); //手機(jī)型號(hào)
sb.append("\nRELEASE :").append(Build.VERSION.RELEASE); //frimware版本(系統(tǒng)版本) 如:2.1-update1
sb.append("\nSDK :").append(Build.VERSION.SDK); //sdk版本號(hào)
sb.append("\n_______ OTHER _______");
sb.append("\nBOARD :").append(Build.BOARD); //基板名 如 MSM8974
sb.append("\nPRODUCT :").append(Build.PRODUCT); //The name of the overall product.
sb.append("\nDEVICE :").append(Build.DEVICE); //品牌型號(hào)名额各,如小米4對(duì)應(yīng)cancro
sb.append("\nFINGERPRINT :").append(Build.FINGERPRINT); //包含制造商,設(shè)備名吧恃,系統(tǒng)版本等諸多信息 如 Xiaomi/cancro_wc_lte/cancro:6.0.1/MMB29M/V8.1.3.0.MXDCNDI:user/release-keys
sb.append("\nHOST :").append(Build.HOST); // 如 c3-miui-ota-bd43
sb.append("\nTAGS :").append(Build.TAGS); //Comma-separated tags describing the build, like "unsigned,debug".
sb.append("\nTYPE :").append(Build.TYPE); //The type of build, like "user" or "eng".
sb.append("\nTIME :").append(Build.TIME); //當(dāng)前時(shí)間虾啦,毫秒值
sb.append("\nINCREMENTAL :").append(Build.VERSION.INCREMENTAL);
sb.append("\n_______ CUPCAKE-3 _______");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.CUPCAKE) {
sb.append("\nDISPLAY :").append(Build.DISPLAY); // 如 MMB29M
}
sb.append("\n_______ DONUT-4 _______");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.DONUT) {
sb.append("\nSDK_INT :").append(Build.VERSION.SDK_INT);
sb.append("\nMANUFACTURER :").append(Build.MANUFACTURER); // The manufacturer of the product/hardware. 如 Xiaomi
sb.append("\nBOOTLOADER :").append(Build.BOOTLOADER); //The system bootloader version number. 如
sb.append("\nCPU_ABI :").append(Build.CPU_ABI); // 如 armeabi-v7a
sb.append("\nCPU_ABI2 :").append(Build.CPU_ABI2); // 如 armeabi
sb.append("\nHARDWARE :").append(Build.HARDWARE); // The name of the hardware (from the kernel command line or /proc). 如 qcom
sb.append("\nUNKNOWN :").append(Build.UNKNOWN); // Value used for when a build property is unknown.
sb.append("\nCODENAME :").append(Build.VERSION.CODENAME);
}
sb.append("\n_______ GINGERBREAD-9 _______");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
sb.append("\nSERIAL :").append(Build.SERIAL); // A hardware serial number, if available. 如 abcdefgh
}
return sb.toString();
}
/**
* 獲取手機(jī)的IMEI號(hào)碼
* 使用TelephonyManager時(shí)需要 <uses-permission android:name="READ_PHONE_STATE" />
*/
public static String getPhoneIMEI(Context context) {
TelephonyManager mTm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String imei = mTm.getDeviceId();
return imei;
}
/**
* 獲取手機(jī)的imsi號(hào)碼
* 使用TelephonyManager時(shí)需要 <uses-permission android:name="READ_PHONE_STATE" />
*/
public static String getPhoneIMSI(Context context) {
TelephonyManager mTm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String imsi = mTm.getSubscriberId();
return imsi;
}
/**
* 獲取手機(jī)號(hào)碼
* 使用TelephonyManager時(shí)需要 <uses-permission android:name="READ_PHONE_STATE" />
*/
public static String getPhoneNumer(Context context) {
TelephonyManager mTm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String numer = mTm.getLine1Number(); // 手機(jī)號(hào)碼,有的可得,有的不可得
return numer;
}
}
//# [Android下獲取設(shè)備唯一標(biāo)識(shí)(UDID, DeviceID...)](https://www.cnblogs.com/Nbox1989/p/4346347.html)
protected static final String PREFS_FILE = "qy_gank_device_id.xml";
protected static final String PREFS_DEVICE_ID = "qy_gank_device_id";
protected static String uuid;
static public String getUDID(Context s_instance) {
if (uuid == null) {
synchronized (PhoneUtils.class) {
if (uuid == null) {
final SharedPreferences prefs = s_instance.getSharedPreferences(PREFS_FILE, 0);
final String id = prefs.getString(PREFS_DEVICE_ID, null);
if (id != null) {
// Use the ids previously computed and stored in the prefs file
uuid = id;
} else {
final String androidId = Settings.Secure.getString(s_instance.getContentResolver(), Settings.Secure.ANDROID_ID);
// Use the Android ID unless it's broken, in which case fallback on deviceId,
// unless it's not available, then fallback on a random number which we store
// to a prefs file
try {
if (!"9774d56d682e549c".equals(androidId)) {
uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8")).toString();
} else {
final String deviceId = ((TelephonyManager) s_instance.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
uuid = deviceId != null ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")).toString() : UUID.randomUUID().toString();
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
// Write the value out to the prefs file
prefs.edit().putString(PREFS_DEVICE_ID, uuid).commit();
}
}
}
}
return uuid;
}
后記:2017-10-30傲醉,將代碼稍作了整理蝇闭。
感謝:
Android 獲得設(shè)備狀態(tài)信息、Mac地址硬毕、IP地址
Android編程獲取手機(jī)型號(hào)呻引,本機(jī)電話號(hào)碼,sdk版本號(hào)及firmware版本號(hào)號(hào)(即系統(tǒng)版本號(hào)號(hào))