84 lines
2.4 KiB
C#
84 lines
2.4 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.ConstrainedExecution;
|
|
using UnityEngine;
|
|
|
|
public class DeviceIdHandle
|
|
{
|
|
private static DeviceIdHandle _instance;
|
|
private AndroidJavaObject oaidActivity;
|
|
|
|
private string oaid = "";
|
|
private string imei = "";
|
|
|
|
public string Oaid => oaid;
|
|
public string Imei => imei;
|
|
|
|
class OaidCallbackProxy : AndroidJavaProxy
|
|
{
|
|
public OaidCallbackProxy() : base("com.example.oaidtest2.DemoHelper$OaidCallback") { }
|
|
|
|
void onResult(int code, string msg, string oaid, long durationMs)
|
|
{
|
|
if (code == 0)
|
|
{
|
|
Debug.Log($"OAID: {oaid} (耗时: {durationMs}ms)");
|
|
DeviceIdHandle.Instance.oaid = oaid;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"OAID 获取失败: {msg}");
|
|
}
|
|
}
|
|
}
|
|
|
|
class IMEICallbackProxy : AndroidJavaProxy
|
|
{
|
|
public IMEICallbackProxy() : base("com.example.oaidtest2.DemoHelper$ImeiCallback") { }
|
|
void onResult(string imei)
|
|
{
|
|
DeviceIdHandle.Instance.imei = imei;
|
|
}
|
|
}
|
|
|
|
public static DeviceIdHandle Instance
|
|
{
|
|
get
|
|
{
|
|
if (_instance is null)
|
|
{
|
|
_instance = new DeviceIdHandle();
|
|
}
|
|
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
#if UNITY_ANDROID && !UNITY_EDITOR
|
|
//获取当前 Android Activity 并调用插件初始化接口
|
|
try
|
|
{
|
|
var unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
|
|
var activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
|
|
var appContext = activity.Call<AndroidJavaObject>("getApplicationContext");
|
|
|
|
var helperClass = new AndroidJavaClass("com.example.oaidtest2.DemoHelper");
|
|
var helper = helperClass.CallStatic<AndroidJavaObject>("getInstance", appContext);
|
|
|
|
helper.Call("getOAID", new OaidCallbackProxy());
|
|
|
|
// 调用 Android 的 GetIMEI 方法
|
|
helper.Call("getImei", new IMEICallbackProxy());
|
|
}
|
|
catch (System.Exception e)
|
|
{
|
|
Debug.LogError("GetDeviceId failed: " + e);
|
|
}
|
|
#else
|
|
Debug.Log("GetDeviceID: Initialized (editor or non-Android). Android call skipped.");
|
|
#endif
|
|
}
|
|
}
|