亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法在 android 中獲取 Advertising ID Provider

無法在 android 中獲取 Advertising ID Provider

慕仙森 2023-06-08 20:19:52
我想為我的應用獲取廣告 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我寧愿有一個不需要依賴的解決方案
查看完整描述

4 回答

?
慕容708150

TA貢獻1831條經驗 獲得超4個贊

試試這個代碼..對我來說工作..


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'

}


查看完整回答
反對 回復 2023-06-08
?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

將以下庫添加到您的 gradle 中,然后從那里獲取 AdId。它非常適合我

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


查看完整回答
反對 回復 2023-06-08
?
繁華開滿天機

TA貢獻1816條經驗 獲得超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'

}


查看完整回答
反對 回復 2023-06-08
?
小唯快跑啊

TA貢獻1863條經驗 獲得超2個贊

您使用以下方法獲取設備的 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);

? ? ? ? ? ? }

? ? ? ? }

? ? });

}


查看完整回答
反對 回復 2023-06-08
  • 4 回答
  • 0 關注
  • 461 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號