猿问

无法在 android 中获取 Advertising ID Provider

我想为我的应用获取广告 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
我宁愿有一个不需要依赖的解决方案


慕仙森
浏览 333回答 4
4回答

慕容708150

试试这个代码..对我来说工作..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 {&nbsp;@Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main);AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected String doInBackground(Void... params) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AdvertisingIdClient.Info idInfo = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; idInfo = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (GooglePlayServicesNotAvailableException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (GooglePlayServicesRepairableException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String advertId = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; advertId = idInfo.getId();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }catch (NullPointerException e){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return advertId;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; protected void onPostExecute(String advertId) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(getApplicationContext(), advertId, Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; task.execute();}}build.gradle 添加依赖dependencies {&nbsp; &nbsp; &nbsp; &nbsp; implementation 'com.google.android.gms:play-services-ads-lite:18.1.1'}

冉冉说

将以下库添加到您的 gradle 中,然后从那里获取 AdId。它非常适合我 &nbsp; &nbsp;implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0'

繁华开满天机

以下是Kotlin.首先,将此规则添加到build.gradle依赖的应用程序模块级文件中:implementation 'com.google.android.gms:play-services-ads-lite:11.8.0'这是 AddUtilsJava 类:package com.example.myapplicationimport android.content.Contextimport android.os.AsyncTaskimport android.util.Logimport com.google.android.gms.ads.identifier.AdvertisingIdClientimport com.google.android.gms.common.GooglePlayServicesNotAvailableExceptionimport com.google.android.gms.common.GooglePlayServicesRepairableExceptionimport java.io.IOExceptionclass AddUtilsJava : AsyncTask<Context, String, String>() {&nbsp; &nbsp; private fun getIdThread(context: Context): String? {&nbsp; &nbsp; &nbsp; &nbsp; var idInfo: AdvertisingIdClient.Info? = null&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; idInfo = AdvertisingIdClient.getAdvertisingIdInfo(context.applicationContext)&nbsp; &nbsp; &nbsp; &nbsp; } catch (e: GooglePlayServicesNotAvailableException) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace()&nbsp; &nbsp; &nbsp; &nbsp; } catch (e: GooglePlayServicesRepairableException) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace()&nbsp; &nbsp; &nbsp; &nbsp; } catch (e: IOException) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var advertId: String? = null&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; advertId = idInfo!!.id&nbsp; &nbsp; &nbsp; &nbsp; } catch (e: NullPointerException) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace()&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return advertId&nbsp; &nbsp; }&nbsp; &nbsp; override fun doInBackground(vararg contexts: Context): String? {&nbsp; &nbsp; &nbsp; &nbsp; return getIdThread(contexts[0])&nbsp; &nbsp; }&nbsp; &nbsp; override fun onPostExecute(s: String?) {&nbsp; &nbsp; &nbsp; &nbsp; super.onPostExecute(s)&nbsp; &nbsp; &nbsp; &nbsp; if (s != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d(TAG, s)&nbsp; &nbsp; }&nbsp; &nbsp; companion object {&nbsp; &nbsp; &nbsp; &nbsp; internal var TAG = "addUtilsJava"&nbsp; &nbsp; }}这是 build.gradle 文件:dependencies {&nbsp; &nbsp; implementation fileTree(dir: 'libs', include: ['*.jar'])&nbsp; &nbsp; implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"&nbsp; &nbsp; implementation 'androidx.appcompat:appcompat:1.1.0-rc01'&nbsp; &nbsp; implementation 'androidx.cardview:cardview:1.0.0'&nbsp; &nbsp; implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.5'&nbsp; &nbsp; implementation 'com.google.android.gms:play-services-ads-lite:11.8.0' //for ads&nbsp; &nbsp; implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'&nbsp; &nbsp; testImplementation 'junit:junit:4.13-beta-3'&nbsp; &nbsp; androidTestImplementation 'androidx.test:runner:1.3.0-alpha02'&nbsp; &nbsp; androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha02'}

小唯快跑啊

您使用以下方法获取设备的 AAID。在 onCreate 方法中调用下面的方法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);            }        }    });}
随时随地看视频慕课网APP

相关分类

Java
我要回答