我找到了解决方案 请帮助解决初学者的问题。我需要预加载广告并点击“下一步”按钮播放广告(在活动之间的过渡期间)。在我的例子中,它无法正常工作。请正确更正我的代码,我将非常感谢你。
public class Home_5_9Eng extends AppCompatActivity implements View.OnClickListener {
Button btnBack2;
Button btnNext2;
InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_5_9_eng);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
btnBack2 = (Button) findViewById(R.id.btnBack2);
btnBack2.setOnClickListener(this);
btnNext2 = (Button) findViewById(R.id.btnNext2);
btnNext2.setOnClickListener(this);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.window_ad_unit_id));
AdRequest adRequestInter = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequestInter);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btnBack2:
Intent intent = new Intent(this, Home_5_8Eng.class);
startActivity(intent);
break;
case R.id.btnNext2:
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
intent =new Intent(this, Home_5_10Eng.class);
startActivity(intent);
break;
}
finish();
}}
我找到了解决方案
public class Home_5_1 extends AppCompatActivity implements View.OnClickListener {
Button btnNext2;
InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_5_1);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
btnNext2 = (Button) findViewById(R.id.btnNext2);
btnNext2.setOnClickListener(this);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.window_ad_unit_id));
AdRequest adRequestInter = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequestInter);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
AdRequest adRequestInter = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequestInter);
}
Intent intent = new Intent(Home_5_1.this, Home_5_2.class);
startActivity(intent);
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnNext2:
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Intent intent = new Intent(this, Home_5_2.class);
startActivity(intent);
break;
}
finish();
}}}
答案 0 :(得分:0)
您是否已将com.google.android.gms.ads.AdActivity声明添加到您的清单???
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="21" />
<!-- Include required permissions for Google Mobile Ads to run-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!--This meta-data tag is required to use Google Play Services.-->
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".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>
<!--Include the AdActivity configChanges and theme. -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
</application>
答案 1 :(得分:0)
永远不要做网络代码onCreate,拨打电话或服务或AsynTask
创建一个ShowAds()函数并在点击时调用,它将起作用
void ShowAds()
{
// code to show Banner or InterstitialAd
}
添加.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
像
AdRequest adRequestInter = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators
.addTestDevice("Your Device ID") // An example device ID
.build();
仅限开发模式 或者您的Admob帐户将被取消。
删除.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
和
发布前.addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4")
。
评论中的代码:
public class Home_5_1 extends AppCompatActivity implements View.OnClickListener {
Button btnNext2;
InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_5_1);
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
btnNext2 = (Button) findViewById(R.id.btnNext2);
btnNext2.setOnClickListener(this);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.window_ad_unit_id));
AdRequest adRequestInter = new AdRequest.Builder().build(); // Replace this line only with- AdRequest request = new AdRequest.Builder().addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4").build();
mInterstitialAd.loadAd(adRequestInter);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
AdRequest adRequestInter = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequestInter);
}
Intent intent = new Intent(Home_5_1.this, Home_5_2.class);
startActivity(intent);
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnNext2:
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Intent intent = new Intent(this, Home_5_2.class);
startActivity(intent);
break;
}
finish();
}}}
运行此代码时,您的实际设备ID将以详细信息显示 复制并替换为&#34; AC98C820A50B4AD8A2106EDE96FB87D4&#34;然后做进一步的发展。