Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
D
device_id_plugin
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
openSourceLibrary
device_id_plugin
Commits
21ae9c08
提交
21ae9c08
authored
2月 25, 2026
作者:
史晓晨
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix:转换kotlin,修复无法获取设备id问题
上级
1b4c625a
全部展开
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
95 行增加
和
89 行删除
+95
-89
DeviceUtils.kt
...n/kotlin/com/clx/devideId/device_id_plugin/DeviceUtils.kt
+93
-87
SPUtils.kt
.../main/kotlin/com/clx/devideId/device_id_plugin/SPUtils.kt
+0
-0
pubspec.lock
example/pubspec.lock
+2
-2
没有找到文件。
android/src/main/kotlin/com/clx/devideId/device_id_plugin/DeviceUtils.kt
浏览文件 @
21ae9c08
package
com.clx.devideId.device_id_plugin
;
package
com.clx.devideId.device_id_plugin
import
android.annotation.SuppressLint;
import
android.content.Context;
import
android.provider.Settings;
import
android.text.TextUtils;
import
java.util.UUID;
import
android.annotation.SuppressLint
import
android.content.Context
import
android.provider.Settings
import
java.util.UUID
/**
* <pre>
...
...
@@ -14,126 +13,132 @@ import java.util.UUID;
* desc : utils about device
* </pre>
*/
public
final
class
DeviceUtils
{
object
DeviceUtils
{
@SuppressLint
(
"StaticFieldLeak"
)
private
var
mContext
:
Context
?
=
null
private
DeviceUtils
()
{
throw
new
UnsupportedOperationException
(
"u can't instantiate me..."
);
}
private
const
val
KEY_UDID
=
"KEY_UDID"
@
SuppressLint
(
"StaticFieldLeak"
)
private
static
Context
mContext
;
@
Volatile
private
var
udid
:
String
?
=
null
public
static
void
init
(
Context
context
)
{
mContext
=
context
;
/**
* 初始化设备工具类
*/
fun
init
(
context
:
Context
?)
{
if
(
context
==
null
)
return
mContext
=
context
.
applicationContext
}
/**
* Return the android id of device.
*
* @return the android id of device
* 获取 Android ID
*/
@SuppressLint
(
"HardwareIds"
)
public
static
String
getAndroidID
()
{
String
id
=
Settings
.
Secure
.
getString
(
mContext
.
getContentResolver
(),
Settings
.
Secure
.
ANDROID_ID
);
if
(
"9774d56d682e549c"
.
equals
(
id
))
return
""
;
return
id
==
null
?
""
:
id
;
fun
getAndroidID
():
String
{
val
context
=
mContext
?:
return
""
val
id
=
Settings
.
Secure
.
getString
(
context
.
contentResolver
,
Settings
.
Secure
.
ANDROID_ID
)
return
when
{
id
==
"9774d56d682e549c"
->
""
// 过滤无效 Android ID
id
.
isNullOrEmpty
()
->
""
else
->
id
}
}
private
static
final
String
KEY_UDID
=
"KEY_UDID"
;
private
volatile
static
String
udid
;
/**
* Return the unique device id.
* <pre>{1}{UUID(macAddress)}</pre>
* <pre>{2}{UUID(androidId )}</pre>
* <pre>{9}{UUID(random )}</pre>
*
* @return the unique device id
* 获取唯一设备 ID(默认前缀空、使用缓存)
*/
public
static
String
getUniqueDeviceId
()
{
return
getUniqueDeviceId
(
""
,
true
)
;
fun
getUniqueDeviceId
():
String
{
return
getUniqueDeviceId
(
""
,
true
)
}
/**
* Return the unique device id.
* <pre>android 10 deprecated {prefix}{1}{UUID(macAddress)}</pre>
* <pre>{prefix}{2}{UUID(androidId )}</pre>
* <pre>{prefix}{9}{UUID(random )}</pre>
*
* @param prefix The prefix of the unique device id.
* @return the unique device id
* 获取唯一设备 ID(指定前缀、使用缓存)
* @param prefix 设备 ID 前缀
*/
public
static
String
getUniqueDeviceId
(
String
prefix
)
{
return
getUniqueDeviceId
(
prefix
,
true
)
;
fun
getUniqueDeviceId
(
prefix
:
String
?):
String
{
return
getUniqueDeviceId
(
prefix
,
true
)
}
/**
* Return the unique device id.
* <pre>{1}{UUID(macAddress)}</pre>
* <pre>{2}{UUID(androidId )}</pre>
* <pre>{9}{UUID(random )}</pre>
*
* @param useCache True to use cache, false otherwise.
* @return the unique device id
* 获取唯一设备 ID(默认前缀、指定是否使用缓存)
* @param useCache 是否使用缓存
*/
public
static
String
getUniqueDeviceId
(
boolean
useCache
)
{
return
getUniqueDeviceId
(
""
,
useCache
)
;
fun
getUniqueDeviceId
(
useCache
:
Boolean
):
String
{
return
getUniqueDeviceId
(
""
,
useCache
)
}
/**
* Return the unique device id.
* <pre>android 10 deprecated {prefix}{1}{UUID(macAddress)}</pre>
* <pre>{prefix}{2}{UUID(androidId )}</pre>
* <pre>{prefix}{9}{UUID(random )}</pre>
*
* @param prefix The prefix of the unique device id.
* @param useCache True to use cache, false otherwise.
* @return the unique device id
* 获取唯一设备 ID(核心方法)
* @param prefix 设备 ID 前缀
* @param useCache 是否使用缓存
*/
public
static
String
getUniqueDeviceId
(
String
prefix
,
boolean
useCache
)
{
fun
getUniqueDeviceId
(
prefix
:
String
?,
useCache
:
Boolean
):
String
{
val
safePrefix
=
prefix
?:
""
if
(!
useCache
)
{
return
getUniqueDeviceIdReal
(
prefix
);
return
getUniqueDeviceIdReal
(
safePrefix
)
}
if
(
udid
==
null
)
{
synchronized
(
DeviceUtils
.
class
)
{
synchronized
(
DeviceUtils
::
class
.
java
)
{
if
(
udid
==
null
)
{
final
String
id
=
SPUtils
.
getInstance
().
getString
(
KEY_UDID
,
null
);
if
(
i
d
!=
null
)
{
udid
=
id
;
return
udid
;
val
cachedId
=
SPUtils
.
getInstance
().
getString
(
KEY_UDID
,
null
)
if
(
cachedI
d
!=
null
)
{
udid
=
cachedId
return
udid
!!
}
return
getUniqueDeviceIdReal
(
prefix
);
// 缓存不存在则生成新的 UDID
return
getUniqueDeviceIdReal
(
safePrefix
)
}
}
}
return
udid
;
return
udid
?:
""
}
private
static
String
getUniqueDeviceIdReal
(
String
prefix
)
{
try
{
final
String
androidId
=
getAndroidID
();
if
(!
TextUtils
.
isEmpty
(
androidId
))
{
return
saveUdid
(
prefix
+
2
,
androidId
);
/**
* 生成真实的唯一设备 ID(无缓存)
*/
private
fun
getUniqueDeviceIdReal
(
prefix
:
String
):
String
{
return
try
{
val
androidId
=
getAndroidID
()
if
(
androidId
.
isNotEmpty
())
{
saveUdid
(
prefix
+
2
,
androidId
)
}
else
{
saveUdid
(
prefix
+
9
,
""
)
}
}
catch
(
Exception
ignore
)
{
/**/
}
return
saveUdid
(
prefix
+
9
,
""
);
}
catch
(
ignore
:
Exception
)
{
// 异常时生成随机 UUID
saveUdid
(
prefix
+
9
,
""
)
}
}
private
static
String
saveUdid
(
String
prefix
,
String
id
)
{
udid
=
getUdid
(
prefix
,
id
);
SPUtils
.
getInstance
().
put
(
KEY_UDID
,
udid
);
return
udid
;
/**
* 保存 UDID 到 SP 并更新内存缓存(修复:id 判空 + SPUtils 操作加 try-catch)
*/
private
fun
saveUdid
(
prefix
:
String
,
id
:
String
?):
String
{
val
safeId
=
id
?:
""
udid
=
getUdid
(
prefix
,
safeId
)
SPUtils
.
getInstance
().
put
(
KEY_UDID
,
udid
!!
)
return
udid
?:
""
}
private
static
String
getUdid
(
String
prefix
,
String
id
)
{
if
(
id
.
equals
(
""
))
{
return
prefix
+
UUID
.
randomUUID
().
toString
().
replace
(
"-"
,
""
);
/**
* 生成 UDID 字符串
*/
private
fun
getUdid
(
prefix
:
String
?,
id
:
String
?):
String
{
val
safePrefix
=
prefix
?:
""
val
safeId
=
id
?:
""
return
if
(
safeId
.
isEmpty
())
{
// ID 为空时生成随机 UUID
safePrefix
+
UUID
.
randomUUID
().
toString
().
replace
(
"-"
,
""
)
}
else
{
// ID 不为空时基于 ID 生成 UUID
safePrefix
+
UUID
.
nameUUIDFromBytes
(
safeId
.
toByteArray
()).
toString
().
replace
(
"-"
,
""
)
}
return
prefix
+
UUID
.
nameUUIDFromBytes
(
id
.
getBytes
()).
toString
().
replace
(
"-"
,
""
);
}
}
}
\ No newline at end of file
android/src/main/kotlin/com/clx/devideId/device_id_plugin/SPUtils.kt
浏览文件 @
21ae9c08
差异被折叠。
点击展开。
example/pubspec.lock
浏览文件 @
21ae9c08
...
...
@@ -202,10 +202,10 @@ packages:
dependency: transitive
description:
name: source_span
sha256: "
254ee5351d6cb365c859e20ee823c3bb479bf4a293c22d17a9f1bf144ce86f7c
"
sha256: "
56a02f1f4cd1a2d96303c0144c93bd6d909eea6bee6bf5a0e0b685edbd4c47ab
"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.10.
1
"
version: "1.10.
2
"
stack_trace:
dependency: transitive
description:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论