概述
Android中方法
public class UnityPlayerActivity extends Activity
{
@Override protected void onCreate (Bundle savedInstanceState)
{
}
//获取设备id
public String OnGetDeviceID() {
return SDKCommonHelper.OnGetDeviceID(this);
}
public static String getAndroidID(Activity cpGame) {
String id = android.provider.Settings.Secure.getString(
cpGame.getContentResolver(),
android.provider.Settings.Secure.ANDROID_ID
);
return id == null ? "" : id;
}
//获取设备id
public String GetDeviceId(Activity cpGame)
{
String imei = null;
if(cpGame==null)
{
return imei;
}
if (ActivityCompat.checkSelfPermission(cpGame, "android.permission.READ_PHONE_STATE") == 0) {
TelephonyManager tm = (TelephonyManager)cpGame.getSystemService("phone");
imei = tm.getDeviceId();
}
if (imei == null) {
imei = getAndroidID(cpGame);
}
if (imei == null) {
imei = "";
}
CommonTools.LogPrint("======GetDeviceId==mDeviceId====="+imei);
return imei;
}
}
OC中方法
/***获取广告码***/
-(NSString *)getDeviceId
{
NSString *idfa = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
return idfa;
}
#if defined(__cplusplus)
extern "C"{
#endif
const char* _iOS_GetSDKValue(const char* key)
{
NSString* strFunc = [NSString stringWithUTF8String: key];
NSLog(@"%@", [NSString stringWithFormat:@"ios sdk call _iOS_GetSDKValueFzl funcName : %@",strFunc]);
if([strFunc isEqualToString:@"OnGetDeviceID"])
{
NSString* mDeviceIdstr = [[SDKCommonHelper shareSDKCommonHelper] getDeviceId];
return strdup([mDeviceIdstr UTF8String]);
}
return 0;
}
#if defined(__cplusplus)
}
#endif
Unity中方法
/// <summary>
///
获取Android设备号
/// </summary>
/// <returns></returns>
public static string GetDeviceID()
{
#if UNITY_ANDROID
AndroidJavaClass
javaClass = new
AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject
javaObject = javaClass.GetStatic<AndroidJavaObject>("currentActivity");
return javaObject.Call<string>("OnGetDeviceID");
#elif UNITY_IPHONE
return Get("GetLogoByte")
#endif
}
[DllImport("__Internal")]
private static extern string _iOS_GetSDKValue(string key);
public static string Get(string key)
{
return _iOS_GetSDKValue(key);
}
最后
以上就是美满战斗机为你收集整理的Unity在Android平台和iOS平台获取设备id的全部内容,希望文章能够帮你解决Unity在Android平台和iOS平台获取设备id所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复