You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Java获取Cordova app同款Android设备UUID?

How Cordova's device.uuid Works on Android & How to Get the Same ID in Native Java

Let's break this down step by step. The this.device.uuid you're seeing in the Cordova app isn't the IMEI (which requires the restricted permissions you ran into) — it's almost certainly the Android ID from Settings.Secure.ANDROID_ID, and here's the full breakdown:

1. What Cordova's device.uuid Actually Returns

The Cordova cordova-plugin-device (the plugin that powers device.uuid) uses this logic for Android devices:

  • First, it fetches Settings.Secure.ANDROID_ID — a 64-bit hexadecimal string (16 characters long, exactly matching your example 29904bc142b60dce).
  • This ID stays fixed for the same app (same signing key) on the same device/user profile. That aligns with your observation that the ID never changes.
  • Crucially, accessing this ID requires no special permissions — no READ_PRIVILEGED_PHONE_STATE or even READ_PHONE_STATE is needed, which is why the Cordova app can get it without issues.

2. Native Java Code to Fetch the Same ID

You can retrieve this identical ID with a simple, permission-free snippet:

import android.content.Context;
import android.provider.Settings;

public String getCordovaStyleDeviceUuid(Context context) {
    return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
}

Key Notes:

  • On Android 8.0 (API level 26)+: ANDROID_ID is unique per app signature and user. If you change your app's signing key, the ID will update. For a single signed app on the same device/user, it remains stable.
  • On pre-8.0 Android versions: ANDROID_ID was a device-wide value, but some older devices had bugs where it reset or returned a generic fixed string — this is rare in modern devices.

3. Why Your Original TelephonyManager Approach Failed

The tManager.getDeviceId() method returns the device's IMEI (or MEID for CDMA devices), a hardware-specific identifier. Starting in Android 10 (API level 29), third-party apps can no longer access IMEIs directly — the READ_PRIVILEGED_PHONE_STATE permission is locked to system apps only. Cordova avoids this restriction entirely by using the Android ID instead.

内容的提问来源于stack exchange,提问作者Vishal Tiwari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:28:09