安卓开发

安卓获取OAID方法

2021-08-04  本文已影响0人  蓝不蓝编程

背景

安卓10及以上,不再支持获取imei,但是各个app还是需要获取一个唯一的设备id,那怎么办? 工信部牵头搞了个移动安全联盟,里面提供了个方案。主要是让各个手机厂商提供获取方法,他们出个统一sdk方便普通厂商调用。
工信部的sdk:http://www.msa-alliance.cn/col.jsp?id=120
下面还有两个开源的,也一样能获取各大厂商的设备id方案:
https://github.com/gzu-liyujiang/Android_CN_OAID
https://github.com/shuzilm-open-source/Get_Oaid_CNAdid

实际上,大家都是调用各个厂商提供的接口,以调用努比亚的为例:

import android.content.Context
import android.net.Uri
import android.os.Build
import android.os.Bundle

class NubiaDeviceIDHelper(private val mContext: Context) {
    val nubiaID: String?
        get() {
            var oaid: String? = null
            val bundle: Bundle?
            val uri = Uri.parse("content://cn.nubia.identity/identity")
            try {
                if (Build.VERSION.SDK_INT > 17) {
                    val contentProviderClient =
                        mContext.contentResolver.acquireContentProviderClient(uri)
                    bundle = contentProviderClient!!.call("getOAID", null, null)
                    if (contentProviderClient != null) {
                        if (Build.VERSION.SDK_INT >= 24) {
                            contentProviderClient.close()
                        } else {
                            contentProviderClient.release()
                        }
                    }
                } else {
                    bundle = mContext.contentResolver.call(uri, "getOAID", null, null)
                }
                val code = bundle!!.getInt("code", -1)
                if (code == 0) {
                    oaid = bundle.getString("id")
                    return oaid
                }
                return oaid
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return oaid
        }
}

不同厂商的调用方式不一样,详见上述开源项目。

参考资料

https://blog.csdn.net/qq_40460042/article/details/112966136

上一篇下一篇

猜你喜欢

热点阅读