Android Get advertising ID

Source: Internet
Author: User

< disclaimer: Welcome reprint, but please keep the original source of the article >

This article excerpt from:Https://blog.safaribooksonline.com/2014/01/16/advertising-id-android-kitkat/?utm_source=tuicool

Beginning in August, Google would replace the Android ID with the new Google advertising ID.

Because advertising ID is part of the Google Play Services platform, you must first get the Google Play services SDK and S ET up your project. Note that Google provides good documentation on setting up the SDK which includes the following steps:

    1. Copy the Google SDK Library and import it into your project.
    2. Update your app Manifest file.
    3. Create a Proguard exception to prevent Proguard from stripping away required classes.
    4. Ensure Devices has the Google Play services APK.

See Install the Google Play Services SDK for more information.

Once Setup, you'll use com.google.android.gms.ads.identifier the classes in package, shown here:

Class Description
AdvertisingIdClient A Helper Library for retrieval of advertising ID and related information.
AdvertisingIdClient.Info Includes both the advertising ID as well as the limit ad tracking setting.

To get the advertising ID, you must first get a AdvertisingIdClient.Info object by calling the method getAdvertisingIdInfo() . Note that this must is done with its own thread and is on the main thread or you'll get a IllegalStateException . The following code snippet shows an example of what to get the advertising ID:

1 Importcom.google.android.gms.ads.identifier.AdvertisingIdClient;2 ImportCom.google.android.gms.ads.identifier.AdvertisingIdClient.Info;3 :4 :5 6Info Adinfo =NULL;7 8 Try {9Adinfo =Advertisingidclient.getadvertisingidinfo (mcontext);Ten}Catch(IOException e) { One      ... A}Catch(googleplayservicesavailabilityexception e) { -      ... -}Catch(googleplayservicesnotavailableexception e) { the      ... - } -  -String AdId =Adinfo.getid (); +  -:

Once you get the advertising ID of your can use it instead of using the non-anonymous Android device ID.

Summary

Developers has a number of options to generate unique user identifiers. These include Telephony identifiers such as IMEI or the Android Device ID. Starting with Android 4.4, developers can use the advertising ID that provides a user-specific, unique, and resettable ID For advertising, as provided by Google Play Services.

< disclaimer: Welcome reprint, but please keep the original source of the article >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.