4 回答
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'
}
TA贡献1877条经验 获得超1个赞
将以下库添加到您的 gradle 中,然后从那里获取 AdId。它非常适合我
implementation 'com.google.android.gms:play-services-ads-identifier:17.0.0'
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'
}
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);
}
}
});
}
添加回答
举报