如何在cocos2d 获取时间-x中获取手机设备ID

如何在cocos2d-x中获取手机设备ID_百度知道
如何在cocos2d-x中获取手机设备ID
我有更好的答案
与原生的语言进行交互获取。c和c++都是可以调用oc或者java的方法的。
其他类似问题
为您推荐:
cocos2d的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁6706人阅读
android(16)
我们在项目过程中或多或少会使用到设备的唯一识别码,我们希望能够得到一个稳定、可靠的设备唯一识别码。今天我们将介绍几种方式。
&&&&&& 1. DEVICE_ID
假设我们确实需要用到真实设备的标识,可能就需要用到DEVICE_ID。在以前,我们的Android设备是手机,这个DEVICE_ID可以同通过TelephonyManager.getDeviceId()获取,它根据不同的手机设备返回IMEI,MEID或者ESN码,但它在使用的过程中会遇到很多问题:
非手机设备: 如果只带有Wifi的设备或者音乐播放器没有通话的硬件功能的话就没有这个DEVICE_ID权限: 获取DEVICE_ID需要READ_PHONE_STATE权限,但如果我们只为了获取它,没有用到其他的通话功能,那这个权限有点大才小用bug:在少数的一些手机设备上,该实现有漏洞,会返回垃圾,如:zeros或者asterisks的产品
&&&&&&& 2. MAC ADDRESS
我们也可以通过手机的Wifi或者蓝牙设备获取MAC ADDRESS作为DEVICE ID,但是并不建议这么做,因为并不是所有的设备都有Wifi,并且,如果Wifi没有打开,那硬件设备无法返回MAC ADDRESS.
&&&&&&& 3. Serial Number
在Android 2.3可以通过android.os.Build.SERIAL获取,非手机设备可以通过该接口获取。
&&&&&&& 4. ANDROID_ID
ANDROID_ID是设备第一次启动时产生和存储的64bit的一个数,当设备被wipe后该数重置
ANDROID_ID似乎是获取Device ID的一个好选择,但它也有缺陷:
它在Android &=2.1 or Android &=2.3的版本是可靠、稳定的,但在2.2的版本并不是100%可靠的在主流厂商生产的设备上,有一个很经常的bug,就是每个设备都会产生相同的ANDROID_ID:e549c
&&&&&&& 5. Installtion ID : UUID
以上四种方式都有或多或少存在的一定的局限性或者bug,在这里,有另外一种方式解决,就是使用UUID,该方法无需访问设备的资源,也跟设备类型无关。
这种方式是通过在程序安装后第一次运行后生成一个ID实现的,但该方式跟设备唯一标识不一样,它会因为不同的应用程序而产生不同的ID,而不是设备唯一ID。因此经常用来标识在某个应用中的唯一ID(即Installtion ID),或者跟踪应用的安装数量。很幸运的,Google Developer Blog提供了这样的一个框架:
public class Installation {
&&& private static String sID =
&&& private static final String INSTALLATION = &INSTALLATION&;
&&& public synchronized static String id(Context context) {
&&&&&&& if (sID == null) {&
&&&&&&&&&&& File installation = new File(context.getFilesDir(), INSTALLATION);
&&&&&&&&&&& try {
&&&&&&&&&&&&&&& if (!installation.exists())
&&&&&&&&&&&&&&&&&&& writeInstallationFile(installation);
&&&&&&&&&&&&&&& sID = readInstallationFile(installation);
&&&&&&&&&&& } catch (Exception e) {
&&&&&&&&&&&&&&& throw new RuntimeException(e);
&&&&&&&&&&& }
&&&&&&& return sID;
&&& private static String readInstallationFile(File installation) throws IOException {
&&&&&&& RandomAccessFile f = new RandomAccessFile(installation, &r&);
&&&&&&& byte[] bytes = new byte[(int) f.length()];
&&&&&&& f.readFully(bytes);
&&&&&&& f.close();
&&&&&&& return new String(bytes);
&&& private static void writeInstallationFile(File installation) throws IOException {
&&&&&&& FileOutputStream out = new FileOutputStream(installation);
&&&&&&& String id = UUID.randomUUID().toString();
&&&&&&& out.write(id.getBytes());
&&&&&&& out.close();
综合以上所述,为了实现在设备上更通用的获取设备唯一标识,我们可以实现这样的一个类,为每个设备产生唯一的UUID,以ANDROID_ID为基础,在获取失败时以TelephonyManager.getDeviceId()为备选方法,如果再失败,使用UUID的生成策略。
重申下,以下方法是生成Device ID,在大多数情况下Installtion ID能够满足我们的需求,但是如果确实需要用到Device ID,那可以通过以下方式实现:
import android.content.C
import android.content.SharedP
import android.provider.Settings.S
import android.telephony.TelephonyM
import java.io.UnsupportedEncodingE
import java.util.UUID;
public class DeviceUuidFactory {
&&& protected static final String PREFS_FILE = &device_id.xml&;
&&& protected static final String PREFS_DEVICE_ID = &device_id&;
&&& protected static UUID
&&& public DeviceUuidFactory(Context context) {
&&&&&&& if( uuid ==null ) {
&&&&&&&&&&& synchronized (DeviceUuidFactory.class) {
&&&&&&&&&&&&&&& if( uuid == null) {
&&&&&&&&&&&&&&&&&&& final SharedPreferences prefs = context.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 = UUID.fromString(id);
&&&&&&&&&&&&&&&&&&& } else {
&&&&&&&&&&&&&&&&&&&&&&& final String androidId = Secure.getString(context.getContentResolver(), 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 (!&e549c&.equals(androidId)) {
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& uuid = UUID.nameUUIDFromBytes(androidId.getBytes(&utf8&));
&&&&&&&&&&&&&&&&&&&&&&&&&&& } else {
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& final String deviceId = ((TelephonyManager) context.getSystemService( Context.TELEPHONY_SERVICE )).getDeviceId();
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& uuid = deviceId!=null ? UUID.nameUUIDFromBytes(deviceId.getBytes(&utf8&)) : UUID.randomUUID();
&&&&&&&&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&&&&&& } catch (UnsupportedEncodingException e) {
&&&&&&&&&&&&&&&&&&&&&&&&&&& throw new RuntimeException(e);
&&&&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&&&&&& // Write the value out to the prefs file
&&&&&&&&&&&&&&&&&&&&&&& prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString() ).commit();
&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& }
&&&&&&&&&&& }
&&&& * Returns a unique UUID for the current android device.& As with all UUIDs, this unique ID is &very highly likely&
&&&& * to be unique across all Android devices.& Much more so than ANDROID_ID is.
&&&& * The UUID is generated by using ANDROID_ID as the base key if appropriate, falling back on
&&&& * TelephonyManager.getDeviceID() if ANDROID_ID is known to be incorrect, and finally falling back
&&&& * on a random UUID that's persisted to SharedPreferences if getDeviceID() does not return a
&&&& * usable value.
&&&& * In some rare circumstances, this ID may change.& In particular, if the device is factory reset a new device ID
&&&& * may be generated.& In addition, if a user upgrades their phone from certain buggy implementations of Android 2.2
&&&& * to a newer, non-buggy version of Android, the device ID may change.& Or, if a user uninstalls your app on
&&&& * a device that has neither a proper Android ID nor a Device ID, this ID may change on reinstallation.
&&&& * Note that if the code falls back on using TelephonyManager.getDeviceId(), the resulting ID will NOT
&&&& * change after a factory reset.& Something to be aware of.
&&&& * Works around a bug in Android 2.2 for many devices when using ANDROID_ID directly.
&&&& * @see
&&&& * @return a UUID that may be used to uniquely identify your device for most purposes.
&&& public UUID getDeviceUuid() {
如何获取Android手机的唯一标识?
代码: 这里是你在Android里读出 唯一的 IMSI-ID / IMEI-ID 的方法。
String myIMSI = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMSI);
// within my emulator it returns: 000
String myIMEI = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMEI);
// within my emulator it returns: 000
注:android.os.SystemProperties的标签被打上@hide了,所以sdk中并不会存在。如果需要使用,需要有android的source code支持。
---------------------------------------------------------------------------------------------------------------------------------------------------------
Android 中的几中方法,使用中常常不可靠
1. DEVICE_ID
假设我们确实需要用到真实设备的标识,可能就需要用到DEVICE_ID。通过 TelephonyManager.getDeviceId()获取,它根据不同的手机设备返回IMEI,MEID或者ESN码.
缺点:在少数的一些设备上,该实现有,会返回垃圾数据
&&&&&&& 2. MAC ADDRESS
我们也可以通过Wifi获取MAC ADDRESS作为DEVICE ID
缺点:如果Wifi关闭的时候,硬件设备可能无法返回MAC ADDRESS.。
&&&&&&& 3. Serial Number
android.os.Build.SERIAL直接读取
缺点:在少数的一些设备上,会返回垃圾数据
&&&&&& 4. ANDROID_ID
ANDROID_ID是设备第一次启动时产生和存储的64bit的一个数,
缺点:当设备被wipe后该数改变, 不适用。
android 底层是 ,我们还是用Linux的方法来获取:
文件在: /proc/cpuinfo
通过Adb shell 查看:
adb shell cat /proc/cpuinfo
2 mac 地址
文件路径 /sys/class/net/wlan0/address
adb shell& cat /sys/class/net/wlan0/address&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
xx:xx:xx:xx:xx:aa
这样可以获取两者的序列号,
方法确定,剩下的就是写代码了
以Mac地址为例:
&&&&&&& String getMac() {
&&&&&&&&&&&&&&& String macSerial =
&&&&&&&&&&&&&&& String str = &&;
&&&&&&&&&&&&&&& try {
&&&&&&&&&&&&&&&&&&&&&&& Process pp = Runtime.getRuntime().exec(
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &cat /sys/class/net/wlan0/address &);
&&&&&&&&&&&&&&&&&&&&&&& InputStreamReader ir = new InputStreamReader(pp.getInputStream());
&&&&&&&&&&&&&&&&&&&&&&& LineNumberReader input = new LineNumberReader(ir);
&&&&&&&&&&&&&&&&&&&&&&& for (; null !=) {
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& str = input.readLine();
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& if (str != null) {
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& macSerial = str.trim();// 去空格
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& } catch (IOException ex) {
&&&&&&&&&&&&&&&&&&&&&&& // 赋予默认值
&&&&&&&&&&&&&&&&&&&&&&& ex.printStackTrace();
&&&&&&&&&&&&&&& }
&&&&&&&&&&&&&&& return macS
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:116134次
积分:1241
积分:1241
排名:千里之外
原创:18篇
评论:25条
(3)(2)(1)(1)(1)(2)(1)(1)(4)(9)(2)如何在cocos2d-x中获取手机设备ID_百度知道
如何在cocos2d-x中获取手机设备ID
提问者采纳
你是指唯一标识吗 ?目前的版本 苹果禁掉了大多数的唯一标识获取方式 包括以前的UUID mac地址等 现在我们采用OpenUDID 一个开源的方法 你可以百度每台iOS设备的UDID是唯一且永远不会改变;每台iOS设备的OpenUDID是通过第一个带有OpenUDID SDK包的App生成,如果你完全删除全部带有OpenUDID SDK包的App(比如恢复系统等),那么OpenUDID会重新生成,而且和之前的值会不同,相当于新设备;是否足够替代普通的iOS设备用户不会没事就去恢复系统或者抹掉系统,所以一般OpenUDID的值是不会改变的;在iOS系统升级换代时,会产生较大的影响,毕竟95%以上的iOS设备用户都会选择升级到最新的系统;是否足够替代就看你对UDID的需求是什么了,如果要求怎么都不能变,那OpenUDID可能还是不能满足你的需求!
其他类似问题
为您推荐:
cocos2d的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁cocos2d-x与Android混编实现游戏支付功能
游戏支付平台付实际上就是买卖双方交易过程中的“第三方中介”,也可以说是“技术中介”。这里我选择豌豆荚SDK平台,实现游戏支付功能。
支付流程:配置游戏服务器端回调地址URL游戏客户端使用申请好的Appkey_id,secretkey游戏客户端使用doLogin方法调用豌豆荚登录登录成功(onSuccess)后,游戏客户端提交用户信息(uid,nick,token)到游戏服务器游戏服务器提交uid,token到豌豆荚服务器进行验证,验证通过后游戏完成登录如果有角色创建,游戏客户端调用createRole方法创建角色,注册到豌豆荚服务器,可选调用游戏客户端创建订单,设置游戏订单号(order.out_trade_no),发起支付(pay)玩家完成支付,返回游戏客户端只有支付成功的订单,豌豆荚服务器通知游戏服务器回调地址,包含order.out_trade_no游戏服务器端验证回调信息中的签名,使用RsaTest项目中的公钥既可,验证通过后发放游戏道给游戏客户端并返回success字符串,处理失败返回fail字符串游戏客户端开始使用道。
有关加入豌豆荚SDK具体操作可以参考《技术文档》。
大家先看下,我的目录结构:vcD48cD4gICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIDxpbWcgc3JjPQ=="/uploadfile/Collfiles/.jpg" alt="\" />
是不是和上次博文类似呢?就是cocos2d-x与Android混编实现换“头像图片”!对了,基本实现思路都是一样的,cocos2d-x 控件触发调用android方法实现的。 因它和上篇博文步骤一样,就不多讲了,到时直接上传代码给大家参考吧。
支付功能类:PayDemo.javapackage com.wandoujia.wdpaydemo.
import android.app.A
import android.os.B
import android.os.H
import android.os.L
import android.util.L
import android.view.V
import android.view.View.OnClickL
import android.widget.B
import android.widget.TextV
import com.wandoujia.sdk.plugin.paydef.LoginCallB
import com.wandoujia.sdk.plugin.paydef.PayCallB
import com.wandoujia.sdk.plugin.paydef.U
import com.wandoujia.sdk.plugin.paydef.WandouA
import com.wandoujia.sdk.plugin.paydef.WandouO
import com.wandoujia.sdk.plugin.paydef.WandouP
import com.wandoujia.sdk.plugin.paysdkimpl.PayC
import com.wandoujia.sdk.plugin.paysdkimpl.WandouAccountI
import com.wandoujia.sdk.plugin.paysdkimpl.WandouPayI
import com.wandoujia.wandoujiapaymentplugin.utils.MSG;
public class PayDemo extends Activity implements OnClickListener {
private static final String TAG = "PayDemo";
final String appkey_id = "";
// 开发者 安全秘钥
final String secretkey = "99b4efb45dbe7a1431511";
private WandouAccount account = new WandouAccountImpl();
private WandouPay wandoupay = new WandouPayImpl();
protected void onCreate(Bundle savedInstanceState) {
Log.e(TAG, "start onCreate~~~" + android.os.Build.VERSION.RELEASE);
super.onCreate(savedInstanceState);
//在onCreate中调用PayConfig.init初始化
PayConfig.init(this, appkey_id, secretkey);
gameLayout();
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "start onDestroy~~~" + android.os.Build.VERSION.RELEASE);
public void gameLayout() {
setContentView(R.layout.activity_main);
((Button) findViewById(R.id.pay)).setOnClickListener(this);
((Button) findViewById(R.id.login_button)).setOnClickListener(this);
((Button) findViewById(R.id.logout_button)).setOnClickListener(this);
public String textString(int id) {
return ((TextView) findViewById(id)).getText().toString();
public void setText(final int id, final String str) {
// Request UI update on main thread.
new Handler(Looper.getMainLooper()).post(new Runnable() {
public void run() {
((TextView) findViewById(id)).setText(str);
public void onClick(View v) {
int viewId = v.getId();
if (R.id.pay == viewId) {
Log.w(TAG, "doPayment!");
float money = Float.parseFloat(textString(R.id.money));
long moneyInFen = (long) (money * 100);
WandouOrder order =
new WandouOrder(textString(R.id.subject), textString(R.id.desc), moneyInFen);
// 设置游戏订单号,最长50个字符
order.setOut_trade_no("GameOrderIdMaxLenth50");
// 触发支付
wandoupay.pay(PayDemo.this, order, new PayCallBack() {
public void onSuccess(User user, WandouOrder order) {
Log.w("DemoPay", "onSuccess:" + order);
setText(R.id.orderInfo, user.getNick() + " 支付成功!" + order);
public void onError(User user, WandouOrder order) {
Log.w("DemoPay", "onError:" + order);
setText(R.id.orderInfo, user.getNick() + " 支付失败!" + order);
} else if (R.id.login_button == viewId) {
Log.i(TAG, "PaySdk init.");
// PayConfig.init(this, appkey_id, secretkey);
Log.w(TAG, "doLogin!");
//触发登录
account.doLogin(PayDemo.this, new LoginCallBack() {
public void onSuccess(User user, int type) {
Log.w("login", "success:+" + user);
setText(R.id.account, user.toString());
// 豌豆荚账户UID
Long uid = user.getUid();
// 豌豆荚账户昵称
String nick = user.getNick();
// 豌豆荚账户登录验证 Token ,15分钟内有效
String token = user.getToken();
// 1.请把uid,token 提交游戏服务器
// 2.游戏服务器收到uid,token后提交给豌豆荚服务器验证
// 3.验证通过后,游戏服务器生成一个 cookie 给游戏客户端使用
// 4.游戏客户端得到游戏的cookie 与游戏服务器进行交互通信,保证身份验证安全
public void onError(int returnCode, String info) {
// 请不要在这里重新调用 doLogin
// 游戏界面上应该留有一个登录按钮,来触发 doLogin登录
setText(R.id.account, "Demo中登陆失败:" + MSG.trans(info));
Log.e(TAG, MSG.trans(info));
} else if (R.id.logout_button == viewId) {
Log.w(TAG, "doLogout!");
// doLogout无需处理回掉方法
account.doLogout(getApplicationContext(), null);
// Do the subsequent directly after doLogut was called.
// i.e. Finish your application.
// Then when launching your app next time, user will see the login UI again.
最终运行效果:代码:http://download.csdn.net/detail/my38085
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'iOS 设备唯一 ID 的三种替代方法之一
uniqueIdentifier] 文档中指明的替代方法之一(一共有三个替代方法):
uniqueIdentifier
property Available in iOS 2.0 through iOS 6.1
uniqueIdentifier
属性可用于 iOS 2.0 至 iOS 6.1
uniqueIdentifier
An alphanumeric string unique to each device based on various hardware details. (read-only) (Available in iOS 2.0 through iOS 6.1.
一个基于多种硬件信息的对每一个设备都唯一的数字字母组合串。(只读)(可用于 iOS 2.0 至 iOS 6.1)
替代方法:
Use the identifierForVend or property of this class
使用该类的 identifierForVendor
or the advertisingIdentifier property of the
ASIdentifierManager class instead, as appropriate,
或按需替换为 ASIdentifierManager 类的 advertisingIdentifier 属性
or use the UUID method of the
NSUUID class to create a UUID and write it to the
user defaults database.)
或使用 NSUUID 类的 UUID 方法来创建一个 UUID ,并将它写入用户默认数据库即
UserDefault 中。
- (NSString *)generateUuidString {
CFUUIDRef uuid = CFUUreate(kCFAllocatorDefault);
NSString *uuidString = (NSString*)CFUUIDCreateString(kCFAllocatorDefault, uuid);
[uuidString autorelease];
CFRelease(uuid);
return uuidS
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'

我要回帖

更多关于 cocos 获取设备码 的文章

 

随机推荐