Last active
May 20, 2021 10:32
-
-
Save zhangzhibin/3d839824d44de290bcb2720e115ab62d to your computer and use it in GitHub Desktop.
query device adid for Facebook Ads Testing. contact me at https://xmanyou.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://xmanyou.com | |
// in app gradle file | |
android { | |
// ... | |
compileOptions { | |
sourceCompatibility JavaVersion.VERSION_1_8 | |
targetCompatibility JavaVersion.VERSION_1_8 | |
} | |
} | |
dependencies { | |
// ... | |
// 获取adid | |
implementation 'androidx.ads:ads-identifier:1.0.0-alpha01' | |
// Used for the calls to addCallback() in the snippets on this page. | |
implementation 'com.google.guava:guava:28.0-android' | |
} | |
// java code, in Main Activity | |
private void getADID(){ | |
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(); | |
Log.d("[Main]", "adid ===> " + advertId); | |
}catch (NullPointerException e){ | |
e.printStackTrace(); | |
} | |
return advertId; | |
} | |
@Override | |
protected void onPostExecute(String advertId) { | |
Toast.makeText(getApplicationContext(), advertId, Toast.LENGTH_SHORT).show(); | |
} | |
}; | |
task.execute(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment