Android设备是否有唯一的ID,如果有的话,使用Java访问它的简单方法是什么?
val WIDEVINE_UUID = UUID(-0x121074568629b532L, -0x5c37d8232ae2de13L)
val drumIDByteArray = MediaDrm(WIDEVINE_UUID).getPropertyByteArray(MediaDrm.PROPERTY_DEVICE_UNIQUE_ID)
val drumID = android.util.Base64.encodeToString(drumIDByteArray,android.util.Base64.DEFAULT)
要获取用户id,您可以使用Google Play许可库。
要下载此库,请打开SDK Manager => SDK工具。 下载库文件的路径为:
path_to_android_sdk_on_your_pc/extras/google/market_licensing/library
在项目中包含库(您可以简单地复制其文件)。
接下来,您需要一些实现Policy
接口的方法(您可以简单地使用库中的两个文件之一:ServerManagedPolicy
或StrictPolicy
)。
用户id将在processServerResponse()
函数内提供给您:
public void processServerResponse(int response, ResponseData rawData) {
if(rawData != null) {
String userId = rawData.userId
// use/save the value
}
// ...
}
接下来,您需要使用策略构建LicenseChecker
并调用checkAccess()
函数。可以使用MainActivity.java
作为示例。 MainActivity.java
位于此文件夹中:
path_to_android_sdk_on_your_pc/extras/google/market_licensing/sample/src/com/example/android/market/licensing
不要忘记在AndroidManifest.xml中添加CHECK_LICENSE权限。
有关许可库的更多信息:https://developer.android.com/google/play/licensing
public static String getMacAddr() {
StringBuilder res1 = new StringBuilder();
try {
List<NetworkInterface> all =
Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface nif : all) {
if (!nif.getName().equalsIgnoreCase("p2p0")) continue;
byte[] macBytes = nif.getHardwareAddress();
if (macBytes == null) {
continue;
}
res1 = new StringBuilder();
for (byte b : macBytes) {
res1.append(String.format("%02X:",b));
}
if (res1.length() > 0) {
res1.deleteCharAt(res1.length() - 1);
}
}
} catch (Exception ex) {
}
return res1.toString();
}
package com.aapbd.appbajarlib.common;
import android.os.Build;
import java.util.Locale;
import java.util.UUID;
public class DeviceID {
public static String getDeviceLanguage()
{
Locale locale=Locale.getDefault();
return locale.getDisplayLanguage();
}
public static String getDeviceCountry()
{
Locale locale=Locale.getDefault();
return locale.getDisplayCountry();
}
public static String getDeviceName() {
String manufacturer = Build.MANUFACTURER;
String model = Build.MODEL;
if (model.startsWith(manufacturer)) {
return capitalize(model);
} else {
return capitalize(manufacturer) + " " + model;
}
}
public static String getAndroidVersion() {
String release = Build.VERSION.RELEASE;
int sdkVersion = Build.VERSION.SDK_INT;
return sdkVersion + " (" + release +")";
}
public static int getAndroidAPILevel() {
int sdkVersion = Build.VERSION.SDK_INT;
return sdkVersion;
}
private static String capitalize(String s) {
if (s == null || s.length() == 0) {
return "";
}
char first = s.charAt(0);
if (Character.isUpperCase(first)) {
return s;
} else {
return Character.toUpperCase(first) + s.substring(1);
}
}
/**
* Return pseudo unique ID
* @return ID
*/
public static String getUniquePsuedoID() {
// If all else fails, if the user does have lower than API 9 (lower
// than Gingerbread), has reset their device or 'Secure.ANDROID_ID'
// returns 'null', then simply the ID returned will be solely based
// off their Android device information. This is where the collisions
// can happen.
// Thanks http://www.pocketmagic.net/?p=1662!
// Try not to use DISPLAY, HOST or ID - these items could change.
// If there are collisions, there will be overlapping data
String m_szDevIDShort = "35" + (Build.BOARD.length() % 10) + (Build.BRAND.length() % 10) + (Build.CPU_ABI.length() % 10) + (Build.DEVICE.length() % 10) + (Build.MANUFACTURER.length() % 10) + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10);
// Thanks to @Roman SL!
// https://dev59.com/V3E85IYBdhLWcg3wbS1h#4789483
// Only devices with API >= 9 have android.os.Build.SERIAL
// http://developer.android.com/reference/android/os/Build.html#SERIAL
// If a user upgrades software or roots their device, there will be a duplicate entry
String serial = null;
try {
serial = android.os.Build.class.getField("SERIAL").get(null).toString();
// Go ahead and return the serial for api => 9
return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
} catch (Exception exception) {
// String needs to be initialized
serial = "serial"; // some value
}
// Thanks @Joe!
// https://dev59.com/V3E85IYBdhLWcg3wbS1h#2853253
// Finally, combine the values we have found by using the UUID class to create a unique identifier
return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
}
}
我使用这个已经6年了,一直很好用。
ANDROID_ID
,请务必阅读这个答案和这个漏洞。 - Dheeraj Vepakomma