Android获取OAID设备标识

Android 获取OAID

##获取到oaid
Android获取OAID设备标识_第1张图片

下载链接:
https://gitee.com/niliusha/getoaid_priject

Android获取OAID设备标识_第2张图片

添加依赖
 压缩包中还有一个supplierconfig.json文件,放在assets中。

  implementation files('libs/msa_mdid_1.0.13.aar')
 -keep class com.bun.miitmdid.core.** {*;}

##MyApplication里初始化


  @Override
    public void onCreate() {
        super.onCreate();
 
       //移动安全联盟MSA 初始化
        try {
            JLibrary.InitEntry(mContext);
        } catch (Exception e) {
            e.printStackTrace();
        }
        MiitHelper miitHelper = new MiitHelper(appIdsUpdater);
    }

    private static String oaid;


    private static boolean isSupportOaid=true;
    private static int errorCode;

    public static String getOaid() {
        return oaid;
    }
    public static String getErrorCode() {
        return String.valueOf(errorCode);
    }

    public static boolean isSupportOaid() {
        return isSupportOaid;
    }

    public static void setIsSupportOaid(boolean isSupportOaid) {
        YouMuApplication.isSupportOaid = isSupportOaid;
    }
    public static void setIsSupportOaid(boolean isSupportOaid,int ErrorCode) {
        YouMuApplication.isSupportOaid = isSupportOaid;
        YouMuApplication.errorCode=ErrorCode;
    }

    private MiitHelper.AppIdsUpdater appIdsUpdater = new MiitHelper.AppIdsUpdater() {
        @Override
        public void OnIdsAvalid(@NonNull String ids) {
            LogUtils.e("++++++ids: ", ids);
            oaid = ids;
        }
    };

##工具类


public class MiitHelper implements IIdentifierListener {

    private AppIdsUpdater _listener;
    public MiitHelper(AppIdsUpdater callback){
        _listener=callback;

    }


    public void getDeviceIds(Context cxt){
//        JLibrary.InitEntry(cxt);
        long timeb=System.currentTimeMillis();
        int nres = CallFromReflect(cxt);
//        int nres=DirectCall(cxt);
        long timee=System.currentTimeMillis();
        long offset=timee-timeb;
        if (nres == ErrorCode.INIT_ERROR_DEVICE_NOSUPPORT) {//1008612 不支持的设备
            YouMuApplication.setIsSupportOaid(false, nres);
        } else if (nres == ErrorCode.INIT_ERROR_LOAD_CONFIGFILE) {//1008613 加载配置文件出错
            YouMuApplication.setIsSupportOaid(false, nres);
        } else if (nres == ErrorCode.INIT_ERROR_MANUFACTURER_NOSUPPORT) {//1008611 不支持的设备厂商
            YouMuApplication.setIsSupportOaid(false, nres);
        } else if (nres == ErrorCode.INIT_ERROR_RESULT_DELAY) {//1008614 获取接口是异步的,结果会在回调中返回,回调执行的回调可能在工作线程
            //TODO 这种情况,还不清楚怎么解决呢。暂且先返false
            YouMuApplication.setIsSupportOaid(false, nres);
        } else if (nres == ErrorCode.INIT_HELPER_CALL_ERROR) {//1008615 反射调用出错
            YouMuApplication.setIsSupportOaid(false, nres);
        }
        Log.d(getClass().getSimpleName(),"return value: "+String.valueOf(nres));

    }


    /*
    * 通过反射调用,解决android 9以后的类加载升级,导至找不到so中的方法
    *
    * */
    private int CallFromReflect(Context cxt){
        return MdidSdkHelper.InitSdk(cxt,true,this);
    }

    /*
    * 直接java调用,如果这样调用,在android 9以前没有题,在android 9以后会抛找不到so方法的异常
    * 解决办法是和JLibrary.InitEntry(cxt),分开调用,比如在A类中调用JLibrary.InitEntry(cxt),在B类中调用MdidSdk的方法
    * A和B不能存在直接和间接依赖关系,否则也会报错
    *
    * */
    private int DirectCall(Context cxt){
       MdidSdk sdk = new MdidSdk();
       return sdk.InitSdk(cxt,this);
    }
    @Override
    public void OnSupport(boolean isSupport, IdSupplier _supplier) {
        if(_supplier==null) {
            return;
        }
//        String oaid=_supplier.getOAID();
//        String vaid=_supplier.getVAID();
//        String aaid=_supplier.getAAID();
//        StringBuilder builder=new StringBuilder();
//        builder.append("support: ").append(isSupport?"true":"false").append("\n");
//        builder.append("OAID: ").append(oaid).append("\n");
//        builder.append("VAID: ").append(vaid).append("\n");
//        builder.append("AAID: ").append(aaid).append("\n");
//        String idstext=builder.toString();
        YouMuApplication.setIsSupportOaid(isSupport);
//        if(_listener!=null){
//            _listener.OnIdsAvalid(idstext);
//        }

        String oaid = _supplier.getOAID();
//        _supplier.shutDown();           //关闭接口
        if (_listener != null) {
            _listener.OnIdsAvalid(oaid);
        }
    }
    public interface AppIdsUpdater{
        void OnIdsAvalid(@NonNull String ids);
    }

}

##获取oaid以及判断是否为模拟器

public class DevicesUtil {

    public static String getOAID() {
        String OAID = "";
        String oaid = getOaid();
        if (oaid.contains("失败")) {
            OAID = "";
        } else {
            OAID = oaid;
        }
        return OAID;
    }

    /**
     * 获取Oaid
     *
     * @return oaid或错误码
     */
    public static String getOaid() {
        String idfa;
        if (YouMuApplication.isSupportOaid()) {
            idfa = YouMuApplication.getOaid();
        } else {
            idfa = "获取失败,ErrorCode: " + YouMuApplication.getErrorCode();
        }
        return idfa;
    }


    /**
     * 判断是否是模拟器
     *
     * @return 模拟器
     */
    public static boolean isEmulator() {
        Log.e("DEBUG-WCL", "Build.FINGERPRINT: " + Build.FINGERPRINT
                + ", Build.MODEL: " + Build.MODEL
                + ", Build.MANUFACTURER: " + Build.MANUFACTURER
                + ", Build.BRAND: " + Build.BRAND
                + ", Build.DEVICE: " + Build.DEVICE
                + ", Build.PRODUCT: " + Build.PRODUCT);
        return Build.FINGERPRINT.startsWith("generic")
                || Build.FINGERPRINT.toLowerCase().contains("vbox")
                || Build.FINGERPRINT.toLowerCase().contains("test-keys")
//                || Build.FINGERPRINT.startsWith("unknown") // 魅族MX4: unknown
                || Build.MODEL.contains("google_sdk")
                || Build.MODEL.contains("Emulator")
                || Build.MODEL.contains("Android SDK built for x86")
                || Build.MANUFACTURER.contains("Genymotion")
                || (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
                || "google_sdk".equals(Build.PRODUCT);
    }

    /**
     * 判断是否是模拟器
     *
     * @return 模拟器
     */
    public static int emulator() {
        boolean isEmulator = isEmulator();
        return isEmulator ? 1 : 0;
    }

}

你可能感兴趣的:(Android,android,安卓,移动开发)