How to get Advertising ID in android programmatically

前端 未结 12 1560
迷失自我
迷失自我 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:57

    You can call the below function in onCreate(Bundle savedInstanceState) of the activity

    and in the logcat search for UIDMY then it will display the id like : I/UIDMY: a1cf5t4e-9eb2-4342-b9dc-10cx1ad1abe1

    void getUIDs()
    {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(SplashScreen.this);
                    String myId = adInfo != null ? adInfo.getId() : null;
    
                   Log.i("UIDMY",myId);
                } catch (Exception e) {
                     Toast toast = Toast.makeText(conext, "error occured ", Toast.LENGTH_SHORT);
        toast.setGravity(gravity, 0,0);
        toast.show();
    
                }
            }
        });
    }
    

提交回复
热议问题