【问题标题】:can not get Advertising ID Provider in android无法在 android 中获取广告 ID 提供者
【发布时间】:2019-08-17 07:27:13
【问题描述】:

我想为我的应用获取广告 ID,但没有成功。

import androidx.ads.identifier.AdvertisingIdClient;
import androidx.ads.identifier.AdvertisingIdInfo;

public class addUtilsJava extends AsyncTask<Context, String, String> {

    static String TAG = "addUtilsJava";


    private String getIdThread(Context context) {

        AdvertisingIdInfo adInfo = null;
        try {
            adInfo = AdvertisingIdClient.getAdvertisingIdInfo(context).get();

        } catch (Exception exception) {
            exception.printStackTrace();
        }
        if (adInfo != null) {
            final boolean isLAT = adInfo.isLimitAdTrackingEnabled();
            return adInfo.getId();
        }
        return null;
    }
    @Override
    protected String doInBackground(Context... contexts) {
        return getIdThread(contexts[0]);
    }
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if (s != null)
            Log.d(TAG, s);
    }
}

它抛出异常提示我androidx.ads.identifier.AdvertisingIdNotAvailableException: No Advertising ID Provider available。我尝试了 2 部手机和模拟器,结果都一样。

在此先感谢

ps:我检查了 android 文档中提供的解决方案,但 它对我不起作用 https://developer.android.com/training/articles/ad-id
我宁愿有一个不需要依赖的解决方案

【问题讨论】:

  • 您是否尝试过不使用支持库的 androidx?你的手机也有播放服务吗?
  • 我没有尝试使用旧的 gms 服务,我所有的手机都有播放服务
  • 您可以访问这些手机上的 Play 商店吗?
  • 我检查了正常手机是否启用了播放服务,我可以在设置中看到 ads-id
  • 我也有同样的问题。我可以使用 com.google.android.gms:play-services-ads-identifier lib 获取广告 ID,但是在尝试使用 androidx lib 时我得到了 AdvertisingIdNotAvailableException。显然,问题出在 androidx lib 本身。

标签: java android kotlin android-asynctask ads


【解决方案1】:

试试这个代码..为我工作..

import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
                    @Override
                    protected String doInBackground(Void... params) {
                        AdvertisingIdClient.Info idInfo = null;
                        try {
                            idInfo = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
                        } catch (GooglePlayServicesNotAvailableException e) {
                            e.printStackTrace();
                        } catch (GooglePlayServicesRepairableException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        String advertId = null;
                        try{
                            advertId = idInfo.getId();
                        }catch (NullPointerException e){
                            e.printStackTrace();
                        }

                        return advertId;
                    }

                    @Override
                    protected void onPostExecute(String advertId) {
                        Toast.makeText(getApplicationContext(), advertId, Toast.LENGTH_LONG).show();
                    }

                };
                task.execute();

}

}

build.gradle 添加依赖

dependencies {
        implementation 'com.google.android.gms:play-services-ads-lite:18.1.1'
}

【讨论】:

  • 问题与AndroidX有关。答案建议使用 Google Play Services 工件。虽然解决方案可能有效 - 这不是原始问题的答案。
【解决方案2】:

将以下库添加到您的 gradle 中,然后从那里获取 AdId。它非常适合我

implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0'

【讨论】:

    【解决方案3】:

    尝试将其与回调一起使用,如training sample 中所述。从主线程进行所有调用。希望对您有所帮助

    【讨论】:

    • 我测试它不起作用,我不想添加依赖只是为了使用它一次
    【解决方案4】:

    以下是Kotlin 中的有效解决方案。

    首先,将此规则添加到您的应用模块级别 build.gradle 文件中的依赖项:
    implementation 'com.google.android.gms:play-services-ads-lite:11.8.0'

    这是 AddUtilsJava 类:

    package com.example.myapplication
    
    
    import android.content.Context
    import android.os.AsyncTask
    import android.util.Log
    import com.google.android.gms.ads.identifier.AdvertisingIdClient
    import com.google.android.gms.common.GooglePlayServicesNotAvailableException
    import com.google.android.gms.common.GooglePlayServicesRepairableException
    import java.io.IOException
    
    class AddUtilsJava : AsyncTask<Context, String, String>() {
    
        private fun getIdThread(context: Context): String? {
            var idInfo: AdvertisingIdClient.Info? = null
            try {
                idInfo = AdvertisingIdClient.getAdvertisingIdInfo(context.applicationContext)
            } catch (e: GooglePlayServicesNotAvailableException) {
                e.printStackTrace()
            } catch (e: GooglePlayServicesRepairableException) {
                e.printStackTrace()
            } catch (e: IOException) {
                e.printStackTrace()
            }
    
            var advertId: String? = null
            try {
                advertId = idInfo!!.id
            } catch (e: NullPointerException) {
                e.printStackTrace()
            }
    
            return advertId
        }
    
        override fun doInBackground(vararg contexts: Context): String? {
            return getIdThread(contexts[0])
        }
    
        override fun onPostExecute(s: String?) {
            super.onPostExecute(s)
            if (s != null)
                Log.d(TAG, s)
        }
    
        companion object {
            internal var TAG = "addUtilsJava"
        }
    }
    

    这是 build.gradle 文件:

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
        implementation 'androidx.cardview:cardview:1.0.0'
        implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
    
        implementation 'com.google.android.gms:play-services-ads-lite:11.8.0' //for ads
    
        implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
        testImplementation 'junit:junit:4.13-beta-3'
        androidTestImplementation 'androidx.test:runner:1.3.0-alpha02'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha02'
    }
    

    【讨论】:

    • 问题与AndroidX有关。答案建议使用 Google Play Services 工件。尽管解决方案可能有效 - 这不是原始问题的答案。
    【解决方案5】:

    您使用以下方法获取设备的 AAID。在 onCreate 方法中调用 bellow 方法

    public void getAAID()
    {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(MyActivity.this);
                    String myId = adInfo != null ? adInfo.getId() : null;
    
                   Log.i("UIDMY",myId);
                } catch (Exception e) {
                     Log.e("error", e);
                }
            }
        });
    }
    

    完整信息:-How to Get AAID for Facebook Ads?

    【讨论】:

      猜你喜欢
      • 2015-08-28
      • 2020-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多