参考链接:Android Studio打包arr包并导入Unity中
参考链接:Unity中调用Android方法
Android UseForUnity .java代码:
package com.android.util;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.UUID;
import static android.content.Context.MODE_PRIVATE;
import static android.content.Context.WIFI_SERVICE;
public class UseForUnity {
private String uuid;
private static final String KEY_UDID = "KEY_UDID";
/// 设置一个 Activity 参数
private static Activity _unityActivity;
// 通过反射获取 Unity 的 Activity 的上下文
public static Activity getActivity() {
if (null == _unityActivity) {
try {
Class classtype = Class.forName("com.unity3d.player.UnityPlayer");
Activity activity = (Activity) classtype.getDeclaredField("currentActivity").get(classtype);
_unityActivity = activity;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
return _unityActivity;
}
/**
* 获取DeviceId
*
* @return the unique device id
*/
@SuppressLint({"MissingPermission", "HardwareIds"})
public String getUniqueDeviceId() {
if (!TextUtils.isEmpty(uuid)) {
return uuid;
}
String localDeviceId = getLocalDeviceId();
if (!TextUtils.isEmpty(localDeviceId)) {
uuid = localDeviceId;
return uuid;
}
String deviced = null;
try {
// 先获取mac
deviced = getMacAddress();
// 为空再获取AndroidId
if (TextUtils.isEmpty(deviced)) {
deviced = getAndroidID();
}
// 为空再获取imei
if (TextUtils.isEmpty(deviced)) {
deviced = getDeviceId();
}
} catch (Exception ignore) {
}
// 都为空,创建1个新的UUID
if (TextUtils.isEmpty(deviced)) {
deviced = getNewUUID();
}
// 保存deviced
if (!TextUtils.isEmpty(deviced)) {
saveDeviceId(deviced);
}
uuid = deviced;
return uuid;
}
/**
* 获取本地DeviceId
*
* @return
*/
private String getLocalDeviceId() {
SharedPreferences mShare = getActivity().getSharedPreferences(KEY_UDID, MODE_PRIVATE);
String localDeviceId = null;
if (mShare != null) {
localDeviceId = mShare.getString(KEY_UDID, null);
}
return localDeviceId;
}
/**
* 获取Mac地址
*
* @return
*/
private String getMacAddress() {
//在Android6.0的版本以后用原来的getMacAddress()方法获取手机的MAC地址都为:02:00:00:00:00:00这个固定的值
String macAddress = getMacAddressByNetworkInterface();
if (isAddressNotInExcepts(macAddress, (String[]) null)) {
return macAddress;
}
macAddress = getMacAddressByInetAddress();
if (isAddressNotInExcepts(macAddress, (String[]) null)) {
return macAddress;
}
macAddress = getMacAddressByWifiInfo();
if (isAddressNotInExcepts(macAddress, (String[]) null)) {
return macAddress;
}
return null;
}
private boolean isAddressNotInExcepts(final String address, final String... excepts) {
if (TextUtils.isEmpty(address)) {
return false;
}
if (excepts == null || excepts.length == 0) {
return !"02:00:00:00:00:00".equals(address);
}
for (String filter : excepts) {
if (address.equals(filter)) {
return false;
}
}
return true;
}
private String getMacAddressByNetworkInterface() {
try {
Enumeration nis = NetworkInterface.getNetworkInterfaces();
while (nis.hasMoreElements()) {
NetworkInterface ni = nis.nextElement();
if (ni == null || !ni.getName().equalsIgnoreCase("wlan0")) continue;
byte[] macBytes = ni.getHardwareAddress();
if (macBytes != null && macBytes.length > 0) {
StringBuilder sb = new StringBuilder();
for (byte b : macBytes) {
sb.append(String.format("%02x:", b));
}
return sb.substring(0, sb.length() - 1);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "02:00:00:00:00:00";
}
private String getMacAddressByInetAddress() {
try {
InetAddress inetAddress = getInetAddress();
if (inetAddress != null) {
NetworkInterface ni = NetworkInterface.getByInetAddress(inetAddress);
if (ni != null) {
byte[] macBytes = ni.getHardwareAddress();
if (macBytes != null && macBytes.length > 0) {
StringBuilder sb = new StringBuilder();
for (byte b : macBytes) {
sb.append(String.format("%02x:", b));
}
return sb.substring(0, sb.length() - 1);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "02:00:00:00:00:00";
}
private InetAddress getInetAddress() {
try {
Enumeration nis = NetworkInterface.getNetworkInterfaces();
while (nis.hasMoreElements()) {
NetworkInterface ni = nis.nextElement();
// To prevent phone of xiaomi return "10.0.2.15"
if (!ni.isUp()) continue;
Enumeration addresses = ni.getInetAddresses();
while (addresses.hasMoreElements()) {
InetAddress inetAddress = addresses.nextElement();
if (!inetAddress.isLoopbackAddress()) {
String hostAddress = inetAddress.getHostAddress();
if (hostAddress.indexOf(':') < 0) return inetAddress;
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return null;
}
private String getMacAddressByWifiInfo() {
try {
final WifiManager wifi = (WifiManager) getActivity()
.getApplicationContext().getSystemService(WIFI_SERVICE);
if (wifi != null) {
final WifiInfo info = wifi.getConnectionInfo();
if (info != null) return info.getMacAddress();
}
} catch (Exception e) {
e.printStackTrace();
}
return "02:00:00:00:00:00";
}
/**
* 获取AndroidId
*
* @return
*/
private String getAndroidID() {
String id = Settings.Secure.getString(
getActivity().getContentResolver(),
Settings.Secure.ANDROID_ID
);
if ("9774d56d682e549c".equals(id)) {
return null;
} else {
return id;
}
}
/**
* 获取DeviceId
*
* @return
*/
private String getDeviceId() {
TelephonyManager tm = (TelephonyManager) getActivity().getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = tm.getDeviceId();
if (deviceId == null) {
return null;
} else {
return deviceId;
}
}
/**
* 获得1个新的UUID
*
* @return
*/
private String getNewUUID() {
return UUID.randomUUID().toString();
}
/**
* 保存DevideId
*
* @param diviceid
*/
private void saveDeviceId(String diviceid) {
SharedPreferences mShare = getActivity().getSharedPreferences(KEY_UDID, MODE_PRIVATE);
if (mShare != null) {
mShare.edit().putString(KEY_UDID, diviceid).commit();
}
}
}