LoginSignup
34
34

More than 5 years have passed since last update.

Android広告IDを取得してみる

Posted at

Androidアプリで広告用に各社色んなIDを使っているかと思うけど、2014年8月1日以降のGooglePlayに申請のあるアプリは、advertising IDを使用しなきゃダメだよっていう規約になっております。というわけで、どんな感じでadvertising IDが取得できるのかやってみます。

参考

おそらく、Android2.3以降でないと対応できない…と思われます。

Android広告IDを取得してみる

AdvertisingID(以降、広告IDと表記統一)の実装は、Googleのドキュメントにも書かれているように、とても簡単。注意しなくちゃいけないのは、メインスレッド以外で取得してねっていう所と、Google Play Servicesが必要っていう所。

Google Play Servicesの対応

EclipseなどからAndroid SDK Managerを起動して、Extrasの中にあるGoogle Play Servicesを選択肢てインストールします。

Google Play Servicesのインストール

Android_SDK_Manager_と_Java_-_Sample_src_jp_basicinc_match_MainActivity_java_-_Eclipse_-__Users_zaru_Documents_workspace.png

Google Play Servicesのインポート

EclipseのFile -> Import -> Existing Android Code Into Workspaceを選択。下記ディレクトリをコピーインポート。

  • /android_sdk/extras/google/google_play_services/libproject

あとは、対象のAndroidアプリプロジェクトのプロパティを開き、Androidの項目で、ライブラリとしてGoogle Play Servicesを読み込みます。

Properties_for_Sample_と_Java_-_Sample_src_jp_basicinc_match_MainActivity_java_-_Eclipse_-__Users_zaru_Documents_workspace.png

これで前準備は完了。

広告IDを取得する

メインスレッドじゃダメだよって書いてあるので、new Threadして取得します。

MainActivity.java
public void getAdId() {
    Thread adIdThread = new Thread(new Runnable() {
        @Override
        public void run() {
            Info adInfo = null;
            try {
                adInfo = AdvertisingIdClient
                        .getAdvertisingIdInfo(getApplicationContext());
                final String id = adInfo.getId();
                final boolean isLAT = adInfo.isLimitAdTrackingEnabled();
                Log.d("DEBUG", "AndroidAdID : " + id);
                Log.d("DEBUG", "OptoutFlag : " + String.valueOf(isLAT));
            } catch (Exception e) {
            }
        }
    });
    adIdThread.start();
}

参考 : https://developer.android.com/google/play-services/id.html

isLimitAdTrackingEnabled() というのは、各ユーザが「Google設定」アプリから設定が可能な「インタレストベース広告をオプトアウト」にチェックをつけているかどうかのフラグです。

この広告ID自体を無効にするっていうのはできないっぽい…。リセットが精一杯の抵抗ってことになるのかな。

34
34
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
34
34