我创建了一个带有3个标签(片段)的android应用。
我在“搜索”标签中添加了非页内广告。这意味着当我单击“搜索”标签时,插页式广告将会弹出。但问题是3秒钟后插页式广告加载。 AdMob政策是错误的。如何纠正?我想点击“搜索”标签时立即加载广告。
这是我的代码:
MainActivity.java
public class MainActivity extends AppCompatActivity {
public InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
bottomNav.setOnNavigationItemSelectedListener(navListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_favorites:
selectedFragment = new FavoritesFragment();
break;
case R.id.nav_search:
selectedFragment = new SearchFragment();
break;
} getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
//display code
private void displayInterstitial() {
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
});
}
// display code end
}
SearchFragment.java
public class SearchFragment extends Fragment {
public InterstitialAd mInterstitialAd;
public static ViewPager viewPager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_search, container, false);
//fragment Interstitial ad code start
final AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd = new InterstitialAd(getActivity());
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
if(mInterstitialAd.isLoaded()){
mInterstitialAd.show();
}
}
@Override
public void onAdClosed() {
}
});
//fragment Interstitial ad code end
return v;
}
}
答案 0 :(得分:1)
由于网络速度,网络连接性,Admob的服务器等原因,几乎不可能立即加载InterstitialAd。
这就是为什么InterstitialAds必须先加载,然后在您希望展示时展示。
当前,您正在通过SearchFragment的onCreateView()
方法加载InterstitialAd,然后在加载后立即显示它。这实际上是错误的方法。
相反,您应该更早地加载广告,例如在MainActivity的onCreate()
方法中。然后,在SearchFragment的切换情况下,检查是否已加载InterstitialAd。如果是,请显示它。
这是我的意思:
public class MainActivity extends AppCompatActivity {
public InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadInterstitial();
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
bottomNav.setOnNavigationItemSelectedListener(navListener);
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_favorites:
selectedFragment = new FavoritesFragment();
break;
case R.id.nav_search:
selectedFragment = new SearchFragment();
displayInterstitial();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
private void loadInterstitial(){
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ADMOB_INTERSTITIAL_ID");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});
}
private void displayInterstitial() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
当广告关闭时,您还应该加载下一个InterstitialAd,以便下次按下SearchFragment时可以使用。
此外,请勿将您的Admob单元ID粘贴到此处显示的代码中。那不是您应该分享的信息。