Android Code Snippet Advertising id

Status
Not open for further replies.
1. Add a reference to FirebaseAdMob2.
2. Add to manifest editor:
B4X:
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)
3. Add:
B4X:
Private Sub GetAdvertisingId As ResumableSub
   Dim jo As JavaObject = Me
   jo.RunMethod("GetAdvertisingId", Null)
   Wait For AdvertisingId_Ready (Success As Boolean, Id As String)
   Return Id
End Sub

4a. B4XPages project (in the relevant page):
B4X:
#if Java
import java.util.concurrent.Callable;
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.ads.identifier.AdvertisingIdClient.Info;

public void GetAdvertisingId() {
   BA.runAsync(ba, mostCurrent, "advertisingid_ready", new Object[] {false, ""}
       , new Callable<Object[]>() {
                   @Override
                   public Object[] call() throws Exception {
                       String id = AdvertisingIdClient.getAdvertisingIdInfo(ba.context).getId();
                       return new Object[] {true, id};
                   }
               }); }
#End If

4b. Non-B4XPages project - in the Activity:
B4X:
#if Java
import java.util.concurrent.Callable;
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
import com.google.android.gms.ads.identifier.AdvertisingIdClient.Info;

public static void GetAdvertisingId() {
   BA.runAsync(processBA, mostCurrent, "advertisingid_ready", new Object[] {false, ""}
       , new Callable<Object[]>() {
                   @Override
                   public Object[] call() throws Exception {
                       String id = AdvertisingIdClient.getAdvertisingIdInfo(mostCurrent).getId();
                       return new Object[] {true, id};
                   }
               }); }
#End If

Usage example:
B4X:
Wait For (GetAdvertisingId) Complete (Id As String)
If Id <> "" Then
   Log(Id)
End If

Q: What is the advertising id?

A: https://support.google.com/googleplay/android-developer/answer/6048248?hl=en

The IDE will show a warning about FirebaseAdMob not being used. You can discard this warning with:
B4X:
#IgnoreWarnings: 32
 
Last edited:

Star-Dust

Expert
Licensed User
Longtime User
Thanks Erel and really very useful.
I had tried to do something like this by copying the example Java code but without result.
 
D

Deleted member 103

Guest
The procedure is clear, what I do not understand is what do I do with the "advertising id"?

B4X:
Wait For (GetAdvertisingId) Complete (Id As String)
If Id <> "" Then
   Log(Id)
End If
 
D

Deleted member 103

Guest
Maybe I do not see the forest for the trees (German proverb). :(
My question is: where do I build this ID, should the admod-id be replaced with it?
 

Star-Dust

Expert
Licensed User
Longtime User
Maybe I do not see the forest for the trees (German proverb). :(
My question is: where do I build this ID, should the admod-id be replaced with it?
I would use it as alternative to identifying ID.
The user rarely resets it because he often does not even know it exists.
So it could be used to identify the device.

PS. I did not understand the proverb
 
Last edited:

JackKirk

Well-Known Member
Licensed User
Longtime User
I've managed to steer clear of Firebase up till now but looks like I am going to be forced to use it to get a unique user Id in Android 10+ instead of IMEI.

Could somebody spell out what Erel means in post #1 by:

1. Add a reference to FirebaseAdMob.

Thanks...
 
Last edited:

Jorge M A

Well-Known Member
Licensed User
Longtime User
1. Add a reference to FirebaseAdMob in Libraries Manager ?

upload_2019-12-22_20-36-6.png
 

JackKirk

Well-Known Member
Licensed User
Longtime User
That did the trick - thanks heaps...
 

pompierecattivo

Member
Licensed User
Longtime User
Hello,
probably i did something wrong, but i dont know how...

After add parts as Erel describe on first post, i've tried to compile. Unfortunately compiling ends by a timeout. No explication about. The only thing i see is a over use (i think) of cpu and memory on activity manager window. I attach a screnshot.
Sorry for bad english, any idea appreciate.

View attachment 89320
 

drgottjr

Expert
Licensed User
Longtime User
this doesn't answer "why" you have the problem, but have you tried to reset the timeout? i think it defaults to 1 minute, but you can increase it
 

Attachments

  • timeout.jpg
    timeout.jpg
    120.3 KB · Views: 633

pompierecattivo

Member
Licensed User
Longtime User
Thanks for answer!
My timeout option was set at 180sec (in the past i updated this value from 1 minute). Now is set for 5 min waiting but still timing out.
The strange thing is my app compiled and run correctly before...
 

Attachments

  • 2020-02-29 (3).png
    2020-02-29 (3).png
    15.4 KB · Views: 662

pompierecattivo

Member
Licensed User
Longtime User
Ok,
appear to be something linked to manifest... I try to put my changes step by step. I'm started from manifest where i add the line Erel suggest on first post. The error appear just for this... (of course i also added Firebase AdMob library) I'll Attach my manifest, i know i'm not a good programmer, but any help appreciate.


Manifest:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddPermission(android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
AddPermission(android.permission.FOREGROUND_SERVICE)
AddPermission(android.permission.CAMERA)
AddPermission(android.permission.FLASHLIGHT)
AddPermission(android.permission.ACCESS_WIFI_STATE)
AddPermission(android.permission.CHANGE_WIFI_STATE)
AddManifestText(
<uses-sdk android:minSdkVersion="16" />
<uses-sdk android:targetSdkVersion="29" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS_FULL" android:protectionLevel="signature" />
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
   
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)
AddApplicationText(
<uses-library
      android:name="org.apache.http.legacy"
      android:required="false" />
)
CreateResource(xml, provider_paths,
   <external-files-path name="name" path="shared" />
)
CreateResourceFromFile(Macro, FirebaseAnalytics.GooglePlayBase)

'End of default text.
 
Last edited:

pompierecattivo

Member
Licensed User
Longtime User
Hello Jorge
unfortunately still no news...
I've tryed multidex on main module. And also download Dexer.jar as Erel suggest here but no luck.
I've search on b4a website for a running example for get advertsing id to check if its run and see difference, but nothing found..
 
Status
Not open for further replies.
Top