{"id":1292,"date":"2023-03-18T10:19:57","date_gmt":"2023-03-18T02:19:57","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1292"},"modified":"2023-04-29T09:19:05","modified_gmt":"2023-04-29T01:19:05","slug":"android-10-api-29-new-feature-adaptation-device-hardware-identifier-access-restrictions","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/android-10-api-29-new-feature-adaptation-device-hardware-identifier-access-restrictions\/","title":{"rendered":"Android 10(Api 29)\u65b0\u7279\u6027\u9002\u914d &#8211; \u8bbe\u5907\u786c\u4ef6\u6807\u8bc6\u7b26\u8bbf\u95ee\u9650\u5236"},"content":{"rendered":"<h2>\u8bbe\u5907\u786c\u4ef6\u6807\u8bc6\u7b26\u8bbf\u95ee\u9650\u5236<\/h2>\n<p>\u9650\u5236\u5e94\u7528\u8bbf\u95ee\u4e0d\u53ef\u91cd\u8bbe\u7684\u8bbe\u5907\u8bc6\u522b\u7801\uff0c\u5982 IMEI\u3001\u5e8f\u5217\u53f7\u7b49\uff0c\u7cfb\u7edf\u5e94\u7528\u4e0d\u53d7\u5f71\u54cd\u3002<\/p>\n<h3>\u539f\u6765\u7684\u505a\u6cd5<\/h3>\n<p><!-- more --><\/p>\n<pre><code class=\"language-java\">\/\/ \u5728Android Q\u4e0a\u4ee5\u4e0b\u65b9\u6cd5\u90fd\u4f1a\u6709\u95ee\u9898\n\/\/ \u8fd4\u56de\uff1a866976045261713\nTelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);\ntm.getDeviceId();\ntm.getSubscriberId();\ntm.getDeviceId(TelephonyManager.PHONE_TYPE_NONE);\n\/\/\u8fd4\u56de\uff1a66J0218B19000977\nBuild.getSerial();<\/code><\/pre>\n<p>\uff081\uff09\u5728\u4f4e\u4e8eAndroid Q\u7684\u7cfb\u7edf\u4e0a\u6ca1\u95ee\u9898<\/p>\n<p>\uff082\uff09\u5728Android Q\u53ca\u4ee5\u4e0a\u7684\u7cfb\u7edf\u4e0a\u8fd0\u884c\u65f6\uff1a<\/p>\n<ul>\n<li>\n<p>\u5982<code>targetSdkVersion&lt;Q<\/code>\uff0c\u8fd4\u56denull\u6216\u201cunknown\u201d<\/p>\n<\/li>\n<li>\n<p>\u5982<code>targetSdkVersion&gt;=Q<\/code>\uff0c\u629b\u5f02\u5e38\uff1a<\/p>\n<\/li>\n<\/ul>\n<pre><code>SecurityException: getDeviceId: The user 10196 does not meet the requirements to access device identifiers.<\/code><\/pre>\n<p>\uff083\uff09\u53d7\u5f71\u54cd\u7684\u65b9\u6cd5<\/p>\n<ul>\n<li><code>Build<\/code>\n<ul>\n<li>\n<ul>\n<li><code>getSerial()<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li><code>TelephonyManager<\/code>\n<ul>\n<li>\n<ul>\n<li><code>getImei()<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li><code>getDeviceId()<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li><code>getMeid()<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li><code>getSimSerialNumber()<\/code><\/li>\n<\/ul>\n<\/li>\n<li>\n<ul>\n<li><code>getSubscriberId()<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>\u66ff\u4ee3\u65b9\u6848<\/h3>\n<p>\uff081\uff09\u65b9\u6848\u4e00<\/p>\n<p>\u4f7f\u7528AndroidId\u4ee3\u66ff\uff0c\u7f3a\u70b9\u662f\u5e94\u7528\u7b7e\u7f72\u5bc6\u94a5\u6216\u7528\u6237\uff08\u5982\u7cfb\u7edf\u6062\u590d\u51fa\u4ea7\u8bbe\u7f6e\uff09\u4e0d\u540c\u8fd4\u56de\u7684Id\u4e0d\u540c\u3002\u4e0e\u5b9e\u9645\u6d4b\u8bd5\u7ed3\u679c\u76f8\u7b26\u3002<br \/>\n\u7ecf\u5b9e\u9645\u6d4b\u8bd5\uff1a\u76f8\u540c\u7b7e\u540d\u5bc6\u94a5\u7684\u4e0d\u540c\u5e94\u7528AndroidId\u76f8\u540c\uff0c\u4e0d\u540c\u7b7e\u540d\u7684\u5e94\u7528AndroidId\u4e0d\u540c\u3002\u6062\u590d\u51fa\u4ea7\u8bbe\u7f6e\u6216\u5347\u7ea7\u7cfb\u7edf\u6ca1\u6d4b\u3002<\/p>\n<pre><code class=\"language-java\">\/\/ \u8fd4\u56de\uff1a496836e3a48d2d9d\nString androidId = Settings.System.getString(context.getContentResolver(),\n        Settings.Secure.ANDROID_ID);<\/code><\/pre>\n<p>\uff082\uff09\u65b9\u6848\u4e8c<\/p>\n<p>\u901a\u8fc7\u786c\u4ef6\u4fe1\u606f\u62fc\u63a5\uff0c\u7f3a\u70b9\u662f\u8fd8\u662f\u4e0d\u80fd\u4fdd\u8bc1\u552f\u4e00\u3002<br \/>\n\u7ecf\u6d4b\u8bd5\uff1a\u4f3c\u4e4e\u4e0e\u65b9\u6848\u4e00\u6bd4\u66f4\u7a33\u5b9a\uff0c\u4e0d\u53d7\u5bc6\u94a5\u5f71\u54cd\uff0c\u4f46\u975e\u5b98\u65b9\u5efa\u8bae\uff0c\u6ca1\u5b89\u5168\u611f\u3002<\/p>\n<pre><code class=\"language-java\">\/**\n * \u83b7\u53d6\u8bbe\u5907ID\n * @param context\n * @return\n *\/\npublic static String getDeviceId(Context context) {\n    String deviceId = &quot;&quot;;\n    if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {\n        deviceId = Md5Util.md5(getUniqueID(context), Crypto.getSecret());\n    } else {\n        String imei = getIMEI(context);\n        String androidId = getAndroidId(context);\n        String deviceSerial = getSerialNo();\n        NLog.i(TAG, &quot;IMEI: &quot; + imei + &quot;, AndroidID: &quot; + androidId + &quot;, DeviceSerial: &quot; + deviceSerial);\n        deviceId = Md5Util.md5(imei + androidId + deviceSerial, Crypto.getSecret());\n    }\n    return deviceId;\n}\n\npublic static String getUniqueID(Context context) {\n    String id = null;\n    final String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);\n    if (!TextUtils.isEmpty(androidId) &amp;&amp; !&quot;9774d56d682e549c&quot;.equals(androidId)) {\n        try {\n            UUID uuid = UUID.nameUUIDFromBytes(androidId.getBytes(&quot;utf8&quot;));\n            id = uuid.toString();\n        } catch (UnsupportedEncodingException e) {\n            e.printStackTrace();\n        }\n    }\n\n    if (TextUtils.isEmpty(id)) {\n        id = getUUID(context);\n    }\n\n    return TextUtils.isEmpty(id) ? UUID.randomUUID().toString() : id;\n}\n\nprivate static String getUUID(Context context) {\n    String serial = null;\n\n    String devIDShort = &quot;35&quot; +\n            Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +\n\n            Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +\n\n            Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +\n\n            Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +\n\n            Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +\n\n            Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +\n\n            Build.USER.length() % 10; \/\/13 \u4f4d\n\n    try {\n        if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {\n            if (context.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {\n                return new UUID(devIDShort.hashCode(), System.currentTimeMillis()).toString();\n            }\n            serial = android.os.Build.getSerial();\n        } else {\n            serial = Build.SERIAL;\n        }\n        \/\/API&gt;=9 \u4f7f\u7528serial\u53f7\n        return new UUID(devIDShort.hashCode(), serial.hashCode()).toString();\n    } catch (Exception exception) {\n        return new UUID(devIDShort.hashCode(), System.currentTimeMillis()).toString(); \/\/\u9ed8\u8ba4\u521d\u59cb\u5316\n    }\n\n    \/\/\u4f7f\u7528\u786c\u4ef6\u4fe1\u606f\u62fc\u51d1\u51fa\u6765\u768415\u4f4d\u53f7\u7801\n    \/\/return new UUID(devIDShort.hashCode(), serial.hashCode()).toString();\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8bbe\u5907\u786c\u4ef6\u6807\u8bc6\u7b26\u8bbf\u95ee\u9650\u5236 \u9650\u5236\u5e94\u7528\u8bbf\u95ee\u4e0d\u53ef\u91cd\u8bbe\u7684\u8bbe\u5907\u8bc6\u522b\u7801\uff0c\u5982 IMEI\u3001\u5e8f\u5217\u53f7\u7b49\uff0c\u7cfb\u7edf\u5e94\u7528\u4e0d\u53d7\u5f71\u54cd\u3002 \u539f\u6765\u7684\u505a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[325],"tags":[],"class_list":["post-1292","post","type-post","status-publish","format-standard","hentry","category-android-adaptation"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1292"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1292\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}