본문 바로가기
Language/Android

[Android] 안드로이드_고유 ID 사용 (feat. ADID(Advertising ID)_GAID))

by 썸머워즈 2019. 11. 10.
반응형



ADID(Advertising ID) 추출하기


ADID


보통 안드로이드에서 ADID 란 GAID(Google Advertising ID)를 의미한다.


구글에서 제공하는 Google Play Service의 API를 이용하여 Ad ID를 얻을 수 있는데,


Google Play Service가 없는 디바이스에서는 사용이 불가능하다.


해당 GAID는 유저식별용으로 사용하기에 아주 적합하다.


(ex. 현장 업무용 본인 단말기 식별용)


이제 해당 ID 를 추출하는 방법에 대해 알아보자.


우선 앞서 말한거처럼 Google Play Service의 API를 사용하기 때문에 


build.gradle 에 들어가


dependencies에서 다음과 같이 선언해준다.


dependencies {
...
implementation 'com.google.android.gms:play-services:11.0.4'
implementation 'com.google.android.gms:play-services-auth:11.0.4'
...
}


AdvertisingIdClient 클래스getAdvertisingIdInfo(Context).getId() 메소드를 통해 ADID를 얻는다.

(주의할 점은 해당 작업을 백그라운드 스레드에서 수행해야한다는 점이다.)


private class GoogleAppIdTask extends AsyncTask<Void, Void, String> {
protected String doInBackground(final Void... params) {
String adId = null;
try {
adId = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext()).getId();
Logger.logDebug("adid : " + adId);
} catch (IllegalStateException ex) {
ex.printStackTrace();
Logger.logError("IllegalStateException");
} catch (GooglePlayServicesRepairableException ex) {
ex.printStackTrace();
Logger.logError("GooglePlayServicesRepairableException");
} catch (IOException ex) {
ex.printStackTrace();
Logger.logError("IOException");
} catch (GooglePlayServicesNotAvailableException ex) {
ex.printStackTrace();
Logger.logError("GooglePlayServicesNotAvailableException");
}
return adId;
}

protected void onPostExecute(String adId) {
//작업 수행
}
}


추가로 개인적으로는


필요에 의해서 직접 저렇게 서술해줄수 있지만 


따로 클래스.java 파일로 객채화 시켜서 하는편이 더 편하기 때문에


따로 클래스 파일을 만들어서 사용해주자.


public class GoogleAppIdTask extends AsyncTask<Void, Void, String> {

protected Context mContext = null;

public GoogleAppIdTask(Context context) {
mContext = context;
}

protected String doInBackground(final Void... params) {
String adId = null;

try {
adId = AdvertisingIdClient.getAdvertisingIdInfo(mContext).getId();
CustomLog.d("EHIS", "adid : " + adId);
} catch (IllegalStateException ex) {
CustomLog.d("EHIS", "IllegalStateException" + ex);
} catch (GooglePlayServicesRepairableException ex) {
CustomLog.d("EHIS", "GooglePlayServicesRepairableException" + ex);
} catch (IOException ex) {
CustomLog.d("EHIS", "IOException" + ex);
} catch (GooglePlayServicesNotAvailableException ex) {
CustomLog.d("EHIS", "GooglePlayServicesNotAvailableException" + ex);
} catch (Exception ex) {
CustomLog.d("EHIS", "Exception" + ex);
}

return adId;
}

protected void onPostExecute(String adId) {

// 수행할 작업

}

public void executeSync() {
// execute
if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.HONEYCOMB) {
this.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
else {
this.execute();
}
}
}

이제 해당 고유 ID를 사용하여 코딩을 해보자.

(실행은 Task 실행하듯이 excute()로 실행시켜주면 된다.)

반응형


댓글

TOP