How to get Advertising ID in android programmatically

前端 未结 12 1574
迷失自我
迷失自我 2020-12-08 04:18

I want to get users Advertising ID programmatically.I used the below code from the developer site.But its not working

         Info adInfo = null;
                   


        
12条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 04:52

    import com.google.android.gms.ads.identifier.AdvertisingIdClient.Info;
    
    
    Info adInfo = null;
    
    try {
         adInfo = AdvertisingIdClient.getAdvertisingIdInfo(mContext);
    } catch (IOException e) {
         e.printStackTrace();
    } catch (GooglePlayServicesAvailabilityException e) {
         e.printStackTrace();
    } catch (GooglePlayServicesNotAvailableException e) {
         e.printStackTrace();
    }
    
    String AdId = adInfo.getId();
    

    You need add gms libs otherwise you cannot get the advertising id. It can be reset by user or when you do a factory reset (at factory reset time the Android id also reset).

提交回复
热议问题