Implementing admob for different activities(为不同的活动实现 admob)
问题描述
我总共有三个活动,我正在为每个活动实现 admob,每个活动都有自己的横幅,当活动发生变化时,另一个活动会因为后台加载广告而挂起一点,有什么办法可以切换时所有活动中都会出现一个横幅,以避免延迟.
I have three activities in total and i am implementing admob for each activity, every activity has its own banner and when the activity is changes the other activity hangs a little because of the ad loading in the background, is there any way that one banner appears in all the activities when switched to avoid delays.
推荐答案
你可以做到,只需在应用程序类中加载广告,然后在任何活动中使用它.
you can do it just load ad in application class and use it in any activity.
你可以下载demo
正如我所做的那样,
应用类
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);
}
}
主要活动
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();
}
}
第二个活动
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
<?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>
主要活动布局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>
第二个活动布局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>
</LinearLayout>
这篇关于为不同的活动实现 admob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为不同的活动实现 admob
基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01