安卓设备获取唯一id解决方案
最近碰到一个需求,需要拿到安卓设备的唯一识别码,然后用于推送,这个码必须唯一,且不能变动,比如卸载软件,或者清除数据 ,也必须保证设备码不变

效果图:上面的图是第一次运行,打印的值,下面的图是卸载后重新运行打印的值

安卓设备获取唯一id解决方案_ide

step1: 引用依赖

implementation 'com.github.gzu-liyujiang:Android_CN_OAID:4.2.4'

step2:清单权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.iosdialogdemo">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />

    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <!--ZXING END-->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />


    <uses-permission android:name="android.permission.WRITE_SETTINGS" />


    <!--esp touch-->
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <!--对讲权限-->
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.RECORD_VIDEO" />

    <!--检测网络状态权限-->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.IosDialogDemo">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

step3: 全局初始化

package com.example.iosdialogdemo;


import android.app.Application;
import android.content.Context;

import com.github.gzuliyujiang.oaid.DeviceIdentifier;


/**
 * 参考源码:https://github.com/gzu-liyujiang/Android_CN_OAID.git
 */

public class MyApplication extends Application{
    public static Context app;

    @Override
    public void onCreate() {
        super.onCreate();
        app=getApplicationContext();

        DeviceIdentifier.register(this);
    }
}

step4: 调用方法,拿到设备码

package com.example.iosdialogdemo;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.github.gzuliyujiang.oaid.DeviceID;
import com.github.gzuliyujiang.oaid.DeviceIdentifier;
import com.github.gzuliyujiang.oaid.IGetter;

public class MainActivity extends Activity {

    private MainActivity selfActivity = MainActivity.this;

    private HintDialog hintDialog = new HintDialog(); // 提示框
    private HintDialog singleHintDialog = new HintDialog(); // 单个提示框
    private PhotoDialog photoDialog = new PhotoDialog(); // 拍照 选择相册

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 获取IMEI,只支持Android 10之前的系统,需要READ_PHONE_STATE权限,可能为空
        String a = DeviceIdentifier.getIMEI(this);
        Log.e("TAG", "获取IMEI:" + a);


        // 获取安卓ID,可能为空
        String b = DeviceIdentifier.getAndroidID(this);
        Log.e("TAG", "获取AndroidID:" + b);


        // 获取数字版权管理ID,可能为空。很鸡肋,在某些手机上还可能造成卡死或闪退,自4.2.7版本后已弃用
        String c = DeviceIdentifier.getWidevineID();
        Log.e("TAG", "获取WidevineID:" + c);


        // 获取伪造ID,根据硬件信息生成,不会为空,有大概率会重复
        String d = DeviceIdentifier.getPseudoID();
        Log.e("TAG", "获取PseudoID:" + d);


        // 获取GUID,随机生成,不会为空
        String e = DeviceIdentifier.getGUID(this);
        Log.e("TAG", "获取GUID:" + e);


        // 是否支持OAID/AAID
        boolean f = DeviceID.supportedOAID(this);
        Log.e("TAG", "是否支持OAID/AAID:" + f);


        // 获取OAID/AAID,同步调用
        String g = DeviceIdentifier.getOAID(this);
        Log.e("TAG", "OAID/AAID:" + g);


        // 获取OAID/AAID,异步回调
        DeviceID.getOAID(this, new IGetter() {
            @Override
            public void onOAIDGetComplete(String result) {
                // 不同厂商的OAID/AAID格式是不一样的,可进行MD5、SHA1之类的哈希运算统一
                Log.e("TAG", "onOAIDGetComplete:" + result);
            }

            @Override
            public void onOAIDGetError(Exception error) {
                // 获取OAID/AAID失败
            }
        });

    }
}

end