问题:
This is my manifest file:
<activity
android:screenOrientation="portrait"
android:name="com.google.android.gms.ads.AdActivity"
android:configCha...
可以将文章内容翻译成中文,广告屏蔽插件会导致该功能失效:
问题:
This is my manifest file:
<activity
android:screenOrientation="portrait"
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
This is my activity:
@Override
public void onBackPressed() {
InterstitialAd interstitialAd= new InterstitialAd(this);
interstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
AdRequest adRequest = new AdRequest.Builder().build();
interstitialAd.loadAd(adRequest);
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
}
@Override
public void onAdClosed() {
super.onAdClosed();
finish();
}
});
}
I am getting an ad screen only 2-3 times while pressing the back button.So,how can I solve it, on every back button press.
回答1:
well try this
InterstitialAd interstitialAd = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blue);
interstitialAd= new InterstitialAd(this);
interstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
AdRequest adRequest = new AdRequest.Builder().build();
interstitialAd.loadAd(adRequest);
}
@Override
public void onBackPressed() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
super.onAdClosed();
finish();
}
});
}else{
super.onBackPressed();
}
}
回答2:
if you added the same code in onStart/onResume remove it or remove code in onBackPressed.
Because When ActivityB takes the foreground, Activity A in onPause and onStop state.
When ActivityA comes back(when Back pressed from activity B), it will get onStart, then it call onResume(Activity A).
回答3:
Problem with your code is you are keeping interstetial ads to load and show both on onback press, loading interstetial add will take time and some times it also fails to load , when it fails to load ads or if no ads are available in admob ads wont be shown
better thing is to load interstitial ads in oncreate method and show them on on back as max suggested in his answer
and also in your code how will user exit from screen is ad is not available?
that case is also covered in max answer so go with his answer . In case ad is not shown you have to consider that ad is not present its not a fault in your code , i will also suggest to use this id while testing it is test ad and its hit rate will be very high
interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712");