GAID(Google’s advertising ID)값 가져오기

2020. 7. 12. 22:36개발자료/Android


반응형

○ 코드

AsyncTask<Void, Void, String> taskAdId = new AsyncTask<Void, Void, String>() {
    @Override
    protected String doInBackground(Void... params) {
        AdvertisingIdClient.Info adInfo = null;
        try {
            adInfo = AdvertisingIdClient.getAdvertisingIdInfo(getApplicationContext());
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        } catch (GooglePlayServicesRepairableException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String strAdId = null;
        try{
            strAdId = adInfo.getId();
            Log.d("TAG", "GAID:"+ strAdId);
        }catch (NullPointerException e){
            e.printStackTrace();
        }

        return strAdId;
    }

    @Override
    protected void onPostExecute(String strAdId) {
        Log.d("TAG", "GAID:"+ strAdId);
    }
};
taskAdId.execute();

※ AsyncTask를 사용하지 않고 바로 코드를 작성해서 사용할 경우 예외가 발생한다.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx.xxx/com.xxx.xxx.MainActivity}: java.lang.IllegalStateException: Calling this from your main thread can lead to deadlock
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
	at android.app.ActivityThread.-wrap11(Unknown Source:0)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
	at android.os.Handler.dispatchMessage(Handler.java:105)
	at android.os.Looper.loop(Looper.java:164)
	at android.app.ActivityThread.main(ActivityThread.java:6938)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.IllegalStateException: Calling this from your main thread can lead to deadlock
	at com.google.android.gms.common.internal.Preconditions.checkNotMainThread(Unknown Source:51)
	at com.google.android.gms.ads.identifier.AdvertisingIdClient.finish(Unknown Source:2)
	at com.google.android.gms.ads.identifier.AdvertisingIdClient.getAdvertisingIdInfo(Unknown Source:87)
	at com.xxx.xxx.MainActivity.onCreate(MainActivity.java:164)
	at android.app.Activity.performCreate(Activity.java:7183)
	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
	... 9 more
반응형