LoginSignup
10
9

More than 5 years have passed since last update.

Androidの広告IDを取得する

Posted at

広告IDとは

androidには端末毎に広告IDというものがあって
google設定アプリ > 広告 から見ることが出来る。

screenshot-160817214621.png

公式ドキュメント

このIDをトラッキングしてユーザに適した広告を配信しているようだ。
トラッキングされるのが嫌な人はオプトアウトをする事も出来るが、
結局アプリからIDは見えてしまうのでアプリを信じるしか無さそう。

取得方法

gradleでplay-services-analyticsをimport

compile 'com.google.android.gms:play-services-analytics:x.y.z'

AdvertisingIdClient.getAdvertisingIdInfo(context)はメインスレッドでは取得出来ないので、別スレッドを指定する必要がある。
ここはRx様々:pray:

Observable.fromCallable { AdvertisingIdClient.getAdvertisingIdInfo(context).id }
            .subscribeOn(Schedulers.computation())
            .subscribe({
               it.id // 広告ID
               it.isLimitAdTrackingEnabled // optoutの設定
            }, {
                // error
            })
10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9