cocos2d-x获取设备信息

DeviceUtils.h  

//
//  DeviceUtils.h
//  gamemoom
//
//  Created by Song on 14-8-20.
//
//

#ifndef __gamemoom__DeviceUtils__
#define __gamemoom__DeviceUtils__

#include <iostream>
using namespace std;

const char* getModel();

int getSdkVersion();

const char* getOsVersion();

const char* getPhoneNumber();


#endif /* defined(__gamemoom__DeviceUtils__) */

DeviceUtils.mm 

Ios端获取设备信息

//
//  DeviceUtils.m
//  gamemoom
//
//  Created by Song on 14-8-20.
//
//

#import "DeviceUtils.h"

const char* getModel() {
    NSString *strModel = [[UIDevice currentDevice] model];
    return [strModel UTF8String];
}

int getSdkVersion() {
    return 0;
}

const char* getOsVersion() {
    NSString *strSysVersion = [[UIDevice currentDevice] systemVersion];
    return [strSysVersion UTF8String];
}

const char* getPhoneNumber() {
    return "";
}

DeviceUtils.cpp

Android端获取设备信息

//
//  DeviceUtils.cpp
//  gamemoom
//
//  Created by Song on 14-8-20.
//
//

#include "DeviceUtils.h"
#include <platform/android/jni/JniHelper.h>

USING_NS_CC;

static const char* JAVA_CLASS_NAME = "com/easygo/games/utils/DeviceUtils";
extern "C"
{
    static const char* jni_GetStaticMethod_string(const char* className, const char* methodName) {
        std::string ret;
        JniMethodInfo t;
        
        if (JniHelper::getStaticMethodInfo(t, className, methodName, "()Ljava/lang/String;")) {
            jstring retFromJava = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
            const char* str = t.env->GetStringUTFChars(retFromJava, 0);
            ret = str;
            
            t.env->ReleaseStringUTFChars(retFromJava, str);
            t.env->DeleteLocalRef(t.classID);
        }
        return ret.c_str();
    }
    
    static int jni_GetStaticMethod_int(const char* className, const char* methodName) {
        int ret = 0;
        JniMethodInfo t;
        
        if (JniHelper::getStaticMethodInfo(t, className, methodName, "()I")) {
            ret = t.env->CallStaticIntMethod(t.classID, t.methodID);
            
            t.env->DeleteLocalRef(t.classID);
        }
        return ret;
    }
}

const char* getModel() {
    return jni_GetStaticMethod_string(JAVA_CLASS_NAME, "getModel");
}

int getSdkVersion() {
    return jni_GetStaticMethod_int(JAVA_CLASS_NAME, "getSdkVersion");
}

const char* getOsVersion() {
    return jni_GetStaticMethod_string(JAVA_CLASS_NAME, "getOsVersion");
}

const char* getPhoneNumber() {
    return jni_GetStaticMethod_string(JAVA_CLASS_NAME, "getPhoneNumber");
}

DeviceUtils.java

Android端的具体实现

package com.easygo.games.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.telephony.TelephonyManager;

/**
 * Device Utils
 * Created by song on 14-8-20.
 */
public class DeviceUtils {

    public static Context context;

    public static String getModel() {
        return Build.MODEL;
    }

    public static String getPhoneNumber() {
        TelephonyManager phoneMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        return phoneMgr.getLine1Number();
    }

    public static int getSdkVersion() {
        return Build.VERSION.SDK_INT;
    }

    public static String getOsVersion() {
        return Build.VERSION.RELEASE;
    }

    public static String getPackageVersionName() {
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            if (null != packageInfo) {
                return packageInfo.versionName;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }
}

 Android获取本机号码,需要在AndroidManifest.xml中添加权限:

<uses-permission android:name=”android.permission.READ_PHONE_STATE” />

参考链接:

Android

Ios

相关推荐