Implement admob for various activities

I have three actions in general, and I implement admob for each type of activity, each type of activity has its own banner, and when the activity changes, the other activity hangs a bit due to the ad loading in the background, is there a way that one the banner appears in all actions when switching to avoid delays.

+6
source share
2 answers

you can do this, just upload the declaration to the application class and use it in any activity.

you can download demo

how am i doing this

Application class

import android.app.Application; import android.view.ViewGroup; import android.widget.LinearLayout; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdSize; import com.google.android.gms.ads.AdView; public class App extends Application { AdView adView; @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); adView = new AdView(this); adView.setAdSize(AdSize.SMART_BANNER); adView.setAdUnitId("ca-app-pub-1267746788642565/8418489933"); // Request for Ads AdRequest adRequest = new AdRequest.Builder().build(); // Load ads into Banner Ads adView.loadAd(adRequest); } public void loadAd(LinearLayout layAd) { // Locate the Banner Ad in activity xml if (adView.getParent() != null) { ViewGroup tempVg = (ViewGroup) adView.getParent(); tempVg.removeView(adView); } layAd.addView(adView); } } 

Primary activity

 public class MainActivity extends Activity { App app; LinearLayout layAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); layAd = (LinearLayout) findViewById(R.id.layad); app = (App) getApplication(); app.loadAd(layAd); Button btnNext = (Button) findViewById(R.id.next); btnNext.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent iNext = new Intent(MainActivity.this, SecondActivity.class); startActivity(iNext); } }); } @Override protected void onResume() { // TODO Auto-generated method stub app.loadAd(layAd); super.onResume(); } } 

Second activity

 public class SecondActivity extends Activity { App app; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); LinearLayout layAd = (LinearLayout) findViewById(R.id.layad); app = (App) getApplication(); app.loadAd(layAd); } } 

xml manifest

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.admobdemo" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:name="com.example.admobdemo.App" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.admobdemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.admobdemo.SecondActivity" android:label="@string/app_name" > </activity> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" /> </application> </manifest> 

core business xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <LinearLayout android:id="@+id/layad" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> <Button android:id="@+id/next" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> 

and second xml action layout

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <LinearLayout android:id="@+id/layad" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> </LinearLayout> 
+14
source

The ideal way is to use snippets for each of your screens . Therefore, you must use a single action that has a single declaration.

If you want to use several actions instead, then the only workaround I know is to use a static method to load ads:

 public class MyAdView { public static void SetAD(AdView adView){ AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .build(); adView.loadAd(adRequest); } } 

Application:

 public class SomeActivity extends Activity { private AdView adView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.caller_main); MyAdView.SetAd((AdView)findViewById(R.id.adView)); } } 
+1
source

Source: https://habr.com/ru/post/984324/


All Articles