Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Admob appID programmatically (19.1.0)?

Tags:

android

admob

Before version 19.1.0, the appID could be set programmatically like this:

MobileAds.initialize (Context context, String appID)

The new method is

initialize(Context, OnInitializationCompleteListener)

Internally, that method passes null as appID:

public static void initialize(Context var0, OnInitializationCompleteListener var1) {
    zzxw.zzqq().zza(var0, (String)null, var1);
}

Does this mean that appID should no longer be set programmatically?

like image 956
activity Avatar asked May 03 '20 15:05

activity


1 Answers

You have to put APPLICATION_ID on manifest file , you can find your App ID in the AdMob UI. For android:value insert your own AdMob App ID in quotes, as shown below.

<manifest>
<application>
    <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
</application>

after that you can change it programmatically

 try {
        ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
        Bundle bundle = ai.metaData;
        String myApiKey = bundle.getString("com.google.android.gms.ads.APPLICATION_ID");
        Log.d(TAG, "Name Found: " + myApiKey);
        ai.metaData.putString("com.google.android.gms.ads.APPLICATION_ID", "ca-app-pub-3940256099942544~3347511713");//you can replace your key APPLICATION_ID here
        String ApiKey = bundle.getString("com.google.android.gms.ads.APPLICATION_ID");
        Log.d(TAG, "ReNamed Found: " + ApiKey);
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "Failed to load meta-data, NameNotFound: " + e.getMessage());
    } catch (NullPointerException e) {
        Log.e(TAG, "Failed to load meta-data, NullPointer: " + e.getMessage());
    }
like image 92
Mujahid Khan Avatar answered Oct 12 '22 02:10

Mujahid Khan