废话就不说了,代码中都有比较详细的注释:
package app.util; import java.io.IOException; import javax.microedition.io.Connector; import javax.microedition.io.ServerSocketConnection; public class MobileInfo { /** * 取得手机的IP地址,特别注意,由于这里使用了可选API,某些手机可能不支持,不支持的后果就是程序血崩 * @return */ public static String getLocalAddress() { String localAddress = ""; ServerSocketConnection ssc = null; try { ssc = (ServerSocketConnection) Connector.open("socket://:1234"); } catch (Exception ex) { return ""; } try { if(ssc != null) { localAddress = ssc.getLocalAddress(); } } catch (IOException e) { return ""; } return localAddress; } /** * 获取到IMEI号码 * @return */ public static String getIMEI() { try{ String paltForm = detectPlatForm(); String imei = null; if (paltForm.equals(PLATFORM_NOKIA)) { imei = System.getProperty("phone.imei"); if (imei == null || "".equals(imei)) { imei = System.getProperty("com.nokia.IMEI"); } if (imei == null || "".equals(imei)) { imei = System.getProperty("com.nokia.mid.imei"); } } else if (paltForm.equals(PLATFORM_SONY_ERICSSON)) { imei = System.getProperty("com.sonyericsson.imei"); } else if (paltForm.equals(PLATFORM_INTENT)) { imei = "Intent"; } else if (paltForm.equals(PLATFORM_LG)) { imei = System.getProperty("com.lg.IMEI"); } else if (paltForm.equals(PLATFORM_MOTOROLA)) { imei = System.getProperty("com.motorola.IMEI"); } else if (paltForm.equals(PLATFORM_SAMSUNG)) { imei = System.getProperty("com.samsung.imei"); } else if (paltForm.equals(PLATFORM_SIEMENS)) { imei = System.getProperty("com.siemens.imei"); } else if (paltForm.equals(PLATFORM_SUN)) { imei = System.getProperty("microedition.platform"); } else if (paltForm.equals(PLATFORM_NOT_DEFINED)) { if (imei == null || "".equals(imei)) { imei = System.getProperty("IMEI"); } } return imei; } catch (Exception e) { return ""; } } private final static String PLATFORM_NOKIA = "Nokia"; private final static String PLATFORM_SONY_ERICSSON = "Sony_Ericsson"; private final static String PLATFORM_SUN = "Sun"; private final static String PLATFORM_INTENT = "Intent"; private final static String PLATFORM_SAMSUNG = "Samsung"; private final static String PLATFORM_MOTOROLA = "Motorola"; private final static String PLATFORM_SIEMENS = "Siemens"; private final static String PLATFORM_LG = "LG"; private final static String PLATFORM_NOT_DEFINED = "Undefine"; private static String detectPlatForm() { try { final String currentPlatform = System.getProperty("microedition.platform"); if (currentPlatform.indexOf("Nokia") != -1) { return PLATFORM_NOKIA; } else if (currentPlatform.indexOf("SonyEricsson") != -1) { return PLATFORM_SONY_ERICSSON; } else if (currentPlatform.indexOf("SunMicrosystems") != -1) { return PLATFORM_SUN; } else if (currentPlatform.indexOf("intent") != -1) { return PLATFORM_INTENT; } } catch (Throwable ex) { try { Class.forName("com.nokia.mid.ui.FullCanvas"); return PLATFORM_NOKIA; } catch (Throwable ex2) { } } // Detecting SAMSUNG try { Class.forName("com.samsung.util.Vibration"); return PLATFORM_SAMSUNG; } catch (Throwable ex) { } // Detecting MOTOROLA try { Class.forName("com.motorola.multimedia.Vibrator"); return PLATFORM_MOTOROLA; } catch (Throwable ex) { try { Class.forName("com.motorola.graphics.j3d.Effect3D"); return PLATFORM_MOTOROLA; } catch (Throwable ex2) { try { Class.forName("com.motorola.multimedia.Lighting"); return PLATFORM_MOTOROLA; } catch (Throwable ex3) { try { Class.forName("com.motorola.multimedia.FunLight"); return PLATFORM_MOTOROLA; } catch (Throwable ex4) { } } } } // try { // if (getKeyName().toUpperCase().indexOf(SOFT_WORD) > -1) { // return PLATFORM_MOTOROLA; // } // } catch (Throwable e) { // try { // if (getKeyName(SOFT_KEY_LEFT_MOTOROLA1).toUpperCase().indexOf(SOFT_WORD) > -1) { // return PLATFORM_MOTOROLA; // } // } catch (Throwable e1) { // try { // if (getKeyName(SOFT_KEY_LEFT_MOTOROLA2).toUpperCase().indexOf(SOFT_WORD) > -1) { // return PLATFORM_MOTOROLA; // } // } catch (Throwable e2) { // } // } // } // Detecting SIEMENS try { Class.forName("com.siemens.mp.io.File"); return PLATFORM_SIEMENS; } catch (Throwable ex) { } // Detecting LG try { Class.forName("mmpp.media.MediaPlayer"); return PLATFORM_LG; } catch (Throwable ex) { try { Class.forName("mmpp.phone.Phone"); return PLATFORM_LG; } catch (Throwable ex1) { try { Class.forName("mmpp.lang.MathFP"); return PLATFORM_LG; } catch (Throwable ex2) { try { Class.forName("mmpp.media.BackLight"); return PLATFORM_LG; } catch (Throwable ex3) { } } } } return PLATFORM_NOT_DEFINED; } /** * 取得系统CLDC版本与MIDP版本 * @return */ public static String getCLDCVersion() { try{ return System.getProperty("microedition.configuration") + "+" + System.getProperty("microedition.profiles"); }catch(Exception e) { return ""; } } /** * 取得运营商 * @return */ public static String getProvider() { try{ String smsc = System.getProperty("wireless.messaging.sms.smsc"); if (smsc != null) { // 移动短信中心号码 if ((smsc.startsWith("134") || smsc.startsWith("+134") || smsc.startsWith("+86134")) ||(smsc.startsWith("135") || smsc.startsWith("+135") || smsc.startsWith("+86135")) ||(smsc.startsWith("136") || smsc.startsWith("+136") || smsc.startsWith("+86136")) ||(smsc.startsWith("137") || smsc.startsWith("+137") || smsc.startsWith("+86137")) ||(smsc.startsWith("138") || smsc.startsWith("+138") || smsc.startsWith("+86138")) ||(smsc.startsWith("139") || smsc.startsWith("+139") || smsc.startsWith("+86139")) ||(smsc.startsWith("150") || smsc.startsWith("+150") || smsc.startsWith("+86150")) ||(smsc.startsWith("151") || smsc.startsWith("+151") || smsc.startsWith("+86151")) ||(smsc.startsWith("152") || smsc.startsWith("+152") || smsc.startsWith("+86152")) ||(smsc.startsWith("157") || smsc.startsWith("+157") || smsc.startsWith("+86157")) ||(smsc.startsWith("158") || smsc.startsWith("+158") || smsc.startsWith("+86158")) ||(smsc.startsWith("159") || smsc.startsWith("+159") || smsc.startsWith("+86159")) ||(smsc.startsWith("187") || smsc.startsWith("+187") || smsc.startsWith("+86187")) ||(smsc.startsWith("188") || smsc.startsWith("+188") || smsc.startsWith("+86188")) ||(smsc.startsWith("147") || smsc.startsWith("+147") || smsc.startsWith("+86147"))) { return "CHINA_MOBILE"; } // 联通短信中心号码 else if (smsc.startsWith("+86130") || smsc.startsWith("0086130")|| smsc.startsWith("130") || smsc.startsWith("+130") ||smsc.startsWith("+86131") || smsc.startsWith("0086131")|| smsc.startsWith("131") || smsc.startsWith("+131") ||smsc.startsWith("+86132") || smsc.startsWith("0086132")|| smsc.startsWith("132") || smsc.startsWith("+132") ||smsc.startsWith("+86155") || smsc.startsWith("0086155")|| smsc.startsWith("155") || smsc.startsWith("+155") ||smsc.startsWith("+86156") || smsc.startsWith("0086156")|| smsc.startsWith("156") || smsc.startsWith("+156") ||smsc.startsWith("+86186") || smsc.startsWith("0086186")|| smsc.startsWith("186") || smsc.startsWith("+186") ||smsc.startsWith("+86145") || smsc.startsWith("0086145")|| smsc.startsWith("145") || smsc.startsWith("+145")) { return "CHINA_UNICOM"; } // 电信短信中心号码 else if (smsc.startsWith("+86133") || smsc.startsWith("133") || smsc.startsWith("+133") ||smsc.startsWith("+86153") || smsc.startsWith("153") || smsc.startsWith("+153") ||smsc.startsWith("+86189") || smsc.startsWith("189") || smsc.startsWith("+189")) { return "CHINA_TELECOM"; } } } catch (Exception e){ return ""; } return ""; } /** * 取得移动通信类型:是否为GSM模式还是其他CDMA等 * @return */ public static String getCommunicationType() { return ""; } /** * 取得网络类型:EDGE/GPRS/UMTS等 */ public static String getNetWorkType() { return ""; } }