现在,有了android 10更新的权限和安全性,我们无法访问用户设备的设备ID和IMEI号,但我想要该设备的一些唯一ID,以便我们可以跟踪用户。
要求是我们要限制/限制一部手机的登录名
答案 0 :(得分:1)
Android 10限制开发人员访问IMEI号码。
您可以通过获取软件ID获得替代解决方案。您可以将软件ID用作唯一ID。请在我在Application中使用以下代码。
公共静态字符串getDeviceId(上下文上下文){
String deviceId;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
deviceId = Settings.Secure.getString(
context.getContentResolver(),
Settings.Secure.ANDROID_ID);
} else {
final TelephonyManager mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephony.getDeviceId() != null) {
deviceId = mTelephony.getDeviceId();
} else {
deviceId = Settings.Secure.getString(
context.getContentResolver(),
Settings.Secure.ANDROID_ID);
}
}
return deviceId;
}
答案 1 :(得分:0)
由于序列号和 IMEI 号码已在 Android 10 及更高版本中弃用, 因此,我们可以使用 READ_PRIVILEGED_PHONE_STATE 找到唯一标识符的 android id。
欲了解更多信息,请点击以下链接。 https://developer.android.com/training/articles/user-data-ids
答案 2 :(得分:0)
getDeviceId() 自 API 级别 26 起已弃用。
“READ_PRIVILEGE_PHONE_STATE”只能被访问 最佳实践建议您应该“避免使用硬件标识符”。用于唯一标识符。您可以使用来自 firebase 的实例 ID,例如 FirebaseInstanceId.getInstance().getId();
var model = compilation.GetSemanticModel(tree);
var methods = root.DescendantNodes().OfType<InvocationExpressionSyntax>();
foreach(var method in methods)
{
if(model.GetSymbolInfo(method).Symbol is IMethodSymbol symbol &&
symbol.ContainingNamespace.Name == "MyProject" &&
symbol.ContainingType.Name == "LocalizationKeys" &&
symbol.Name == "DefineKey")
{
var key = method.ArgumentList.Arguments.FirstOrDefault();
var eng = method.ArgumentList.Arguments.Skip(1).FirstOrDefault();
if(key.Expression is LiteralExpressionSyntax literalKey &&
eng.Expression is LiteralExpressionSyntax literalEng)
{
// "/foo" -> "Hello World!"
// "/bar" -> "Hello World2!"
Console.WriteLine(literalKey + " -> " + literalEng);
}
else
{
// Bonus: detect violation of key definition rule. It is not a literal!
}
}
}
使用 FirebaseInstanceId.getInstance().getId(); 用于 Android 10 以上的 Api 级别
答案 3 :(得分:-1)
Android引入了一个新ID,用于唯一标识设备,称为 Advertisement_Id 。您可以从以下代码实现中的Application类 onCreate 方法中获取此ID。
/** Retrieve the Android Advertising Id
*
* The device must be KitKat (4.4)+
* This method must be invoked from a background thread.
*
* */
public static synchronized String getAdId (Context context) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
return null;
}
AdvertisingIdClient.Info idInfo = null;
try {
idInfo = AdvertisingIdClient.getAdvertisingIdInfo(context);
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String advertId = null;
try{
advertId = idInfo.getId();
}catch (NullPointerException e){
e.printStackTrace();
}
return advertId;
}
对于Kotlin
fun getAdId() {
//Background Task
AsyncTask.execute {
var adInfo: AdvertisingIdClient.Info? = null
try {
adInfo = AdvertisingIdClient.getAdvertisingIdInfo(applicationContext)
if(adInfo!=null){
val id = adInfo!!.getId()
val isLAT = adInfo!!.isLimitAdTrackingEnabled()
PersistData.setStringData(applicationContext, AppConstant.advertId, id)
val advertId = PersistData.getStringData(applicationContext, AppConstant.advertId)
}
} catch (e: IOException) {
// Unrecoverable error connecting to Google Play services (e.g.,
// the old version of the service doesn't support getting AdvertisingId).
} catch (e: GooglePlayServicesAvailabilityException) {
// Encountered a recoverable error connecting to Google Play services.
} catch (e: GooglePlayServicesNotAvailableException) {
// Google Play services is not available entirely.
}
}
}