无法在 android 中获取广告 ID 提供商
can not get Advertising ID Provider in android
我想为我的应用获取广告 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 个手机 phone 和模拟器,结果都一样。
提前致谢
ps:我检查了 android 文档中提供的解决方案,但 它不适用于 我 https://developer.android.com/training/articles/ad-id
我宁愿有一个不需要依赖的解决方案
尝试将其与 training sample 中所述的回调一起使用。从主线程进行所有调用。希望对你有帮助
试试这个代码..为了我的工作..
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'
}
下面是 Kotlin
中的有效解决方案。
首先,将此规则添加到您的应用程序模块级别 build.gradle
依赖文件中:
implementation 'com.google.android.gms:play-services-ads-lite:11.8.0'
这是 AddUtilsJava class:
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'
}
将以下库添加到您的 gradle 并从那里获取 AdId。它非常适合我
implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0'
您使用以下方法获取设备的 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);
}
}
});
}
我想为我的应用获取广告 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 个手机 phone 和模拟器,结果都一样。
提前致谢
ps:我检查了 android 文档中提供的解决方案,但 它不适用于 我 https://developer.android.com/training/articles/ad-id
我宁愿有一个不需要依赖的解决方案
尝试将其与 training sample 中所述的回调一起使用。从主线程进行所有调用。希望对你有帮助
试试这个代码..为了我的工作..
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'
}
下面是 Kotlin
中的有效解决方案。
首先,将此规则添加到您的应用程序模块级别 build.gradle
依赖文件中:
implementation 'com.google.android.gms:play-services-ads-lite:11.8.0'
这是 AddUtilsJava class:
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'
}
将以下库添加到您的 gradle 并从那里获取 AdId。它非常适合我
implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0'
您使用以下方法获取设备的 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);
}
}
});
}