安卓Apk獲取設備信息

1.TelephonyManager類

                   //創建TelephonyManager對象

TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

/**
* 返回電話狀態
*
* CALL_STATE_IDLE 無任何狀態時
* CALL_STATE_OFFHOOK 接起電話時
* CALL_STATE_RINGING 電話進來時
*/
tm.getCallState();
//返回當前移動終端的位置
CellLocation location=tm.getCellLocation();
//請求位置更新,如果更新將產生廣播,接收對象爲註冊LISTEN_CELL_LOCATION的對象,需要的permission名稱爲ACCESS_COARSE_LOCATION。
location.requestLocationUpdate();
/**
* 獲取數據活動狀態
*
* DATA_ACTIVITY_IN 數據連接狀態:活動,正在接受數據
* DATA_ACTIVITY_OUT 數據連接狀態:活動,正在發送數據
* DATA_ACTIVITY_INOUT 數據連接狀態:活動,正在接受和發送數據
* DATA_ACTIVITY_NONE 數據連接狀態:活動,但無數據發送和接受
*/
tm.getDataActivity();
/**
* 獲取數據連接狀態
*
* DATA_CONNECTED 數據連接狀態:已連接
* DATA_CONNECTING 數據連接狀態:正在連接
* DATA_DISCONNECTED 數據連接狀態:斷開
* DATA_SUSPENDED 數據連接狀態:暫停
*/
tm.getDataState();
/**
* 返回當前移動終端的唯一標識
*
* 如果是GSM網絡,返回IMEI;如果是CDMA網絡,返回MEID
*/
tm.getDeviceId();
//返回移動終端的軟件版本,例如:GSM手機的IMEI/SV碼。
tm.getDeviceSoftwareVersion();
//返回手機號碼,對於GSM網絡來說即MSISDN
tm.getLine1Number();
//返回當前移動終端附近移動終端的信息
List<NeighboringCellInfo> infos=tm.getNeighboringCellInfo();
for(NeighboringCellInfo info:infos){
//獲取鄰居小區號
int cid=info.getCid();
//獲取鄰居小區LAC,LAC: 位置區域碼。爲了確定移動臺的位置,每個GSM/PLMN的覆蓋區都被劃分成許多位置區,LAC則用於標識不同的位置區。
info.getLac();
info.getNetworkType();
info.getPsc();
//獲取鄰居小區信號強度
info.getRssi();
}
//返回ISO標準的國家碼,即國際長途區號
tm.getNetworkCountryIso();
//返回MCC+MNC代碼 (SIM卡運營商國家代碼和運營商網絡代碼)(IMSI)
tm.getNetworkOperator();
//返回移動網絡運營商的名字(SPN)
tm.getNetworkOperatorName();
/**
* 獲取網絡類型
*
* NETWORK_TYPE_CDMA 網絡類型爲CDMA
* NETWORK_TYPE_EDGE 網絡類型爲EDGE
* NETWORK_TYPE_EVDO_0 網絡類型爲EVDO0
* NETWORK_TYPE_EVDO_A 網絡類型爲EVDOA
* NETWORK_TYPE_GPRS 網絡類型爲GPRS
* NETWORK_TYPE_HSDPA 網絡類型爲HSDPA
* NETWORK_TYPE_HSPA 網絡類型爲HSPA
* NETWORK_TYPE_HSUPA 網絡類型爲HSUPA
* NETWORK_TYPE_UMTS 網絡類型爲UMTS
*
* 在中國,聯通的3G爲UMTS或HSDPA,移動和聯通的2G爲GPRS或EGDE,電信的2G爲CDMA,電信的3G爲EVDO
*/
tm.getNetworkType();
/**
* 返回移動終端的類型
*
* PHONE_TYPE_CDMA 手機制式爲CDMA,電信
* PHONE_TYPE_GSM 手機制式爲GSM,移動和聯通
* PHONE_TYPE_NONE 手機制式未知
*/
tm.getPhoneType();
//返回SIM卡提供商的國家代碼
tm.getSimCountryIso();
//返回MCC+MNC代碼 (SIM卡運營商國家代碼和運營商網絡代碼)(IMSI)
tm.getSimOperator();
tm.getSimOperatorName();
//返回SIM卡的序列號(IMEI)
tm.getSimSerialNumber();
/**
* 返回移動終端
*
* SIM_STATE_ABSENT SIM卡未找到
* SIM_STATE_NETWORK_LOCKED SIM卡網絡被鎖定,需要Network PIN解鎖
* SIM_STATE_PIN_REQUIRED SIM卡PIN被鎖定,需要User PIN解鎖
* SIM_STATE_PUK_REQUIRED SIM卡PUK被鎖定,需要User PUK解鎖
* SIM_STATE_READY SIM卡可用
* SIM_STATE_UNKNOWN SIM卡未知
*/
tm.getSimState();
//返回用戶唯一標識,比如GSM網絡的IMSI編號
tm.getSubscriberId();
//獲取語音信箱號碼關聯的字母標識。
tm.getVoiceMailAlphaTag();
//返回語音郵件號碼
tm.getVoiceMailNumber();
tm.hasIccCard();
//返回手機是否處於漫遊狀態
tm.isNetworkRoaming();
// tm.listen(PhoneStateListener listener, int events) ;

//解釋:
//IMSI是國際移動用戶識別碼的簡稱(International Mobile Subscriber Identity)
//IMSI共有15位,其結構如下:
//MCC+MNC+MIN
//MCC:Mobile Country Code,移動國家碼,共3位,中國爲460;
//MNC:Mobile NetworkCode,移動網絡碼,共2位
//在中國,移動的代碼爲電00和02,聯通的代碼爲01,電信的代碼爲03
//合起來就是(也是Android手機中APN配置文件中的代碼):
//中國移動:46000 46002
//中國聯通:46001
//中國電信:46003
//舉例,一個典型的IMSI號碼爲460030912121001

//IMEI是International Mobile Equipment Identity (國際移動設備標識)的簡稱
//IMEI由15位數字組成的”電子串號”,它與每臺手機一一對應,而且該碼是全世界唯一的
//其組成爲:
//1. 前6位數(TAC)是”型號覈准號碼”,一般代表機型
//2. 接着的2位數(FAC)是”最後裝配號”,一般代表產地
//3. 之後的6位數(SNR)是”串號”,一般代表生產順序號
//4. 最後1位數(SP)通常是”0″,爲檢驗碼,目前暫備用


2.通過google在framework提供的一個未開放的類:android.os.SystemProperties,使用方法如下:

/**
     * 根據給定Key獲取值.
     * @return 如果不存在該key則返回空字符串
     * @throws IllegalArgumentException 如果key超過32個字符則拋出該異常
     */
    public static String get(Context context, String key) throws IllegalArgumentException {
 
        String ret= "";
 
        try{
 
          ClassLoader cl = context.getClassLoader(); 
          @SuppressWarnings("rawtypes")
          Class SystemProperties = cl.loadClass("android.os.SystemProperties");
 
          //參數類型
          @SuppressWarnings("rawtypes")
              Class[] paramTypes= new Class[1];
          paramTypes[0]= String.class;
 
          Method get = SystemProperties.getMethod("get", paramTypes);
 
          //參數
          Object[] params= new Object[1];
          params[0]= new String(key);
 
          ret= (String) get.invoke(SystemProperties, params);
 
        }catch( IllegalArgumentException iAE ){
            throw iAE;
        }catch( Exception e ){
            ret= "";
            //TODO
        }
 
        return ret;
 
    }
 
    /**
     * 根據Key獲取值.
     * @return 如果key不存在, 並且如果def不爲空則返回def否則返回空字符串
     * @throws IllegalArgumentException 如果key超過32個字符則拋出該異常
     */
    public static String get(Context context, String key, String def) throws IllegalArgumentException {
 
        String ret= def;
 
        try{
 
          ClassLoader cl = context.getClassLoader(); 
          @SuppressWarnings("rawtypes")
          Class SystemProperties = cl.loadClass("android.os.SystemProperties");
 
          //參數類型
          @SuppressWarnings("rawtypes")
              Class[] paramTypes= new Class[2];
          paramTypes[0]= String.class;
          paramTypes[1]= String.class;          
 
          Method get = SystemProperties.getMethod("get", paramTypes);
 
          //參數
          Object[] params= new Object[2];
          params[0]= new String(key);
          params[1]= new String(def);
 
          ret= (String) get.invoke(SystemProperties, params);
 
        }catch( IllegalArgumentException iAE ){
            throw iAE;
        }catch( Exception e ){
            ret= def;
            //TODO
        }
 
        return ret;
 
    }
 
    /**
     * 根據給定的key返回int類型值.
     * @param key 要查詢的key
     * @param def 默認返回值
     * @return 返回一個int類型的值, 如果沒有發現則返回默認值
     * @throws IllegalArgumentException 如果key超過32個字符則拋出該異常
     */
    public static Integer getInt(Context context, String key, int def) throws IllegalArgumentException {
 
        Integer ret= def;
 
        try{
 
          ClassLoader cl = context.getClassLoader(); 
          @SuppressWarnings("rawtypes")
          Class SystemProperties = cl.loadClass("android.os.SystemProperties");
 
          //參數類型
          @SuppressWarnings("rawtypes")
              Class[] paramTypes= new Class[2];
          paramTypes[0]= String.class;
          paramTypes[1]= int.class;  
 
          Method getInt = SystemProperties.getMethod("getInt", paramTypes);
 
          //參數
          Object[] params= new Object[2];
          params[0]= new String(key);
          params[1]= new Integer(def);
 
          ret= (Integer) getInt.invoke(SystemProperties, params);
 
        }catch( IllegalArgumentException iAE ){
            throw iAE;
        }catch( Exception e ){
            ret= def;
            //TODO
        }
 
        return ret;
 
    }
 
    /**
     * 根據給定的key返回long類型值.
     * @param key 要查詢的key
     * @param def 默認返回值
     * @return 返回一個long類型的值, 如果沒有發現則返回默認值
     * @throws IllegalArgumentException 如果key超過32個字符則拋出該異常
     */
    public static Long getLong(Context context, String key, long def) throws IllegalArgumentException {
 
        Long ret= def;
 
        try{
 
          ClassLoader cl = context.getClassLoader();
          @SuppressWarnings("rawtypes")
              Class SystemProperties= cl.loadClass("android.os.SystemProperties");
 
          //參數類型
          @SuppressWarnings("rawtypes")
              Class[] paramTypes= new Class[2];
          paramTypes[0]= String.class;
          paramTypes[1]= long.class;  
 
          Method getLong = SystemProperties.getMethod("getLong", paramTypes);
 
          //參數
          Object[] params= new Object[2];
          params[0]= new String(key);
          params[1]= new Long(def);
 
          ret= (Long) getLong.invoke(SystemProperties, params);
 
        }catch( IllegalArgumentException iAE ){
            throw iAE;
        }catch( Exception e ){
            ret= def;
            //TODO
        }
 
        return ret;
 
    }
 
    /**
     * 根據給定的key返回boolean類型值.
     * 如果值爲 'n', 'no', '0', 'false' or 'off' 返回false.
     * 如果值爲'y', 'yes', '1', 'true' or 'on' 返回true.
     * 如果key不存在, 或者是其它的值, 則返回默認值.
     * @param key 要查詢的key
     * @param def 默認返回值
     * @return 返回一個boolean類型的值, 如果沒有發現則返回默認值
     * @throws IllegalArgumentException 如果key超過32個字符則拋出該異常
     */
    public static Boolean getBoolean(Context context, String key, boolean def) throws IllegalArgumentException {
 
        Boolean ret= def;
 
        try{
 
          ClassLoader cl = context.getClassLoader(); 
          @SuppressWarnings("rawtypes")
          Class SystemProperties = cl.loadClass("android.os.SystemProperties");
 
          //參數類型
          @SuppressWarnings("rawtypes")
              Class[] paramTypes= new Class[2];
          paramTypes[0]= String.class;
          paramTypes[1]= boolean.class;  
 
          Method getBoolean = SystemProperties.getMethod("getBoolean", paramTypes);
 
          //參數     
          Object[] params= new Object[2];
          params[0]= new String(key);
          params[1]= new Boolean(def);
 
          ret= (Boolean) getBoolean.invoke(SystemProperties, params);
 
        }catch( IllegalArgumentException iAE ){
            throw iAE;
        }catch( Exception e ){
            ret= def;
            //TODO
        }
 
        return ret;
 
    }
 
    /**
     * 根據給定的key和值設置屬性, 該方法需要特定的權限才能操作.
     * @throws IllegalArgumentException 如果key超過32個字符則拋出該異常
     * @throws IllegalArgumentException 如果value超過92個字符則拋出該異常
     */
    public static void set(Context context, String key, String val) throws IllegalArgumentException {
 
        try{
 
          @SuppressWarnings("unused")
          DexFile df = new DexFile(new File("/system/app/Settings.apk"));
          @SuppressWarnings("unused")
          ClassLoader cl = context.getClassLoader(); 
          @SuppressWarnings("rawtypes")
          Class SystemProperties = Class.forName("android.os.SystemProperties");
 
          //參數類型
          @SuppressWarnings("rawtypes")
              Class[] paramTypes= new Class[2];
          paramTypes[0]= String.class;
          paramTypes[1]= String.class;  
 
          Method set = SystemProperties.getMethod("set", paramTypes);
 
          //參數     
          Object[] params= new Object[2];
          params[0]= new String(key);
          params[1]= new String(val);
 
          set.invoke(SystemProperties, params);
 
        }catch( IllegalArgumentException iAE ){
            throw iAE;
        }catch( Exception e ){
            //TODO
        }
 
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章