2017年8月18日 星期五

0818 cocos2d-x 如何取得手機型號跟其他資訊(android)

//範例程式碼
proj.android-studio\app\src\org\cocos2dx\cpp\AppActivity.java

增加
import android.os.Build;   // 主機版名稱 String board = Build.BOARD;
public class AppActivity extends Cocos2dxActivity {    
    private static String deviceInfo; //回傳手機型號跟系統版本
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        deviceInfo = this.getDeviceInfo();
    }  
        public static String findDeviceInfo() {
        return deviceInfo;
    }
    /**
     * Get Android node and Android version
     * */
    public String getDeviceInfo()
    {
        TelephonyManager manager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
  String model = Build.MODEL; //手機型號
String phoneNum = manager.getLine1Number();//本機電話號碼
String sdkVersion = Build.VERSION.SDK;//SDK版本號
String osVersion = Build.VERSION.RELEASE;//Firmware/OS 版本號
return "Phone:" +model + "  OS:" + osVersion;

    }
   
     
}

///參考文章
http://www.cocos2dev.com/?p=208

今天要獲取用戶反饋意見,我默認將用戶的手機號碼上傳了,結果發現並不是所有的手機能拿到本機號碼。找了下發現原是用戶的SIM卡沒有寫入本機號碼導致的。
Android獲取本機號碼:
TelephonyManager phoneMgr = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String phoneNum = phoneMgr.getLine1Number(); 
AndroidManifest.xml中添加權限:
<uses-permission android:name=」android.permission.READ_PHONE_STATE」 />
剛才也說了,手機號碼不是所有的都能獲取。只是有一部分可以拿到。這個是由於移動運營商沒有把手機號碼的數據寫入到sim卡中。
手機型號 Build.MODEL
String MODEL The end-user-visible name for the end product.
sdk版本 Build.VERSION.SDK
String SDK This constant is deprecated. Use SDK_INT to easily get this as an integer.
frimware版本號(系統版本號) Build.VERSION.RELEASE
String RELEASE The user-visible version string.
獲取手機的其他信息:
private void getPhoneStatus(){
TelephonyManager phoneMgr=(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);      
String model = Build.MODEL; //手機型號
String phoneNum = phoneMgr.getLine1Number();//本機電話號碼
String sdkVersion = Build.VERSION.SDK;//SDK版本號
String osVersion = Build.VERSION.RELEASE;//Firmware/OS 版本號
}
Build中包括 硬件廠商,硬件編號,序列號等很多信息。調用方法也很簡單,和上面類似的。
下面是Google提供的參考表:
StringBOARDThe name of the underlying board, like 「goldfish」.
StringBOOTLOADERThe system bootloader version number.
StringBRANDThe brand (e.g., carrier) the software is customized for, if any.
StringCPU_ABIThe name of the instruction set (CPU type + ABI convention) of native code.
StringCPU_ABI2The name of the second instruction set (CPU type + ABI convention) of native code.
StringDEVICEThe name of the industrial design.
StringDISPLAYA build ID string meant for displaying to the user
StringFINGERPRINTA string that uniquely identifies this build.
StringHARDWAREThe name of the hardware (from the kernel command line or /proc).
StringHOST
StringIDEither a changelist number, or a label like 「M4-rc20」.
StringMANUFACTURERThe manufacturer of the product/hardware.
StringMODELThe end-user-visible name for the end product.
StringPRODUCTThe name of the overall product.
StringRADIOThe radio firmware version number.
StringSERIALA hardware serial number, if available.
StringTAGSComma-separated tags describing the build, like 「unsigned,debug」.
longTIME
StringTYPEThe type of build, like 「user」 or 「eng」.
StringUNKNOWNValue used for when a build property is unknown.
StringUSER

沒有留言:

張貼留言

cocos2dx-lua 建立滑鼠監聽

重要關鍵字  EVENT_MOUSE_SCROLL addEventListenerWithSceneGraphPriority      if IsPc() then --建立滑鼠監聽         local listener = cc.EventListenerMouse...