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);
? ? ? ? ? ? }
? ? ? ? }
? ? });
}
添加回答
舉報