Search code examples
androidkotlinadmobandroid-manifestads

Admob App Open ads - unable to extend the class due to an existing service


I am working on an app where I want to show an App Open ad after the splash screen. As per the documentation, we have to add .MyApplication is the name under the application tag in Manifest file, but I have a service that runs as soon as the app is opened and I cannot replace it.

How could I change it to use the appOpen ads?

Manifest -

<application
android:name=".data.NameValue"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
.

.

. />

Solution

  • You need to maintain one Application class for your app and initialize everything from it.

    In your case, you have to start the service and initialize AdMob from the Application class.

    Manifest.xml

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher">
     .
     .
     .
    </application>
    

    MyApplication.java

    public class MyApplication extends Application {
    
        private static AppOpenManager appOpenManager;
    
        @Override
        protected void attachBaseContext(Context base) {
            super.attachBaseContext(base);
        }
    
    
        public void onCreate() {
            super.onCreate();
            
            // Start your service "data.NameValue" here
            
            // Initilize AdMob
            MobileAds.initialize(
              this,
              new OnInitializationCompleteListener() {
                @Override
                public void onInitializationComplete(InitializationStatus
                    initializationStatus) {}
              });
    
            appOpenManager = new AppOpenManager(this);
    
    
        }
    
    }