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
基础教程推荐
- Maven:无效的目标版本:10 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- JPA惰性列表上的流 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 将 double 转换为 Int,向下舍入 2022-01-01
