android - 谷歌广告 ID 在 Nougat 上返回 Null

标签 android android-7.1-nougat google-advertising-id

我在我的应用程序中使用了广告 ID,代码运行良好,我在运行 Android Nougat 7.1.2 的设备上对其进行了测试。你能指出我应该添加什么才能在牛轧糖中得到它吗?

private static void getAdvertisingID() {
    AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            AdvertisingIdClient.Info idInfo = null;
            try {
                idInfo = AdvertisingIdClient.getAdvertisingIdInfo(_context);
                if (idInfo.isLimitAdTrackingEnabled()){ // check if user has opted out of tracking
                    Log.i("NetworkStateChangeReceiver","Not Found");
                    return "did not found GAID... sorry";
                }
            } catch (GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
            } catch (GooglePlayServicesRepairableException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            String advertId = null;
            try{
                advertId = idInfo.getId();
            }catch (Exception e){
                e.printStackTrace();
            }
            return advertId;
        }
        @Override
        protected void onPostExecute(String advertId) {
            Log.i("NetworkStateChangeReceiver","Found "+advertId);
            UtilHelper.setDeviceIDFA(advertId);
        }
    };
    task.execute();
}

最佳答案

事实证明代码工作正常,唯一的问题是在 Nougat 上调用 onPostExecute 方法需要一些时间,而在运行较低 android 版本的其他设备上这个方法被快速调用。我只是在调用 getAdvertisingID 后添加了一些延迟来获取广告 ID,一切正常。 快乐编码

关于android - 谷歌广告 ID 在 Nougat 上返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43446214/

相关文章:

java - 为什么我无法在我的应用程序中使用动态快捷方式?我收到 IllegalStateException : Launcher activity not found for package

android - 仅在牛轧糖上出现 TransactionTooLargeException

android - 应用程序图标启动器未在 Android 7.1.1 中显示

google-analytics - 如何在 Google 测量协议(protocol)中使用广告 ID

android - 如何以编程方式在 Android 中重置 Google 广告 ID?

javascript - 通过移动浏览器访问 Google AAID (Javascript)

android - 如何在按钮顶部创建对话框

android - Kotlin .let {} 空安全

android - 从对话框返回后导航 safeArgs 无法导航

android - 无法在 Windows 8 64 位下安装 Android Studio