java - 如何限制每次 session 的广告数量?

标签 java android admob

我有一个应用程序,在我的应用程序的几个页面上执行一定数量的操作后会显示插页式广告。我担心用户可能会在我的应用中四处走动,然后看到一大堆插页式广告砸在他们脸上。

我如何才能限制每次 session 的广告数量?例如,每个 session 限制 2 或 3 个插页式广告。

谷歌搜索这个问题让我得到了这个唯一相关的结果: https://groups.google.com/forum/#!topic/google-admob-ads-sdk/QZV3Z5Vy4pg

“至于如何做到这一点,例如,您可以在共享首选项中存储一个计数器,并在每次显示插页式广告时递增它。如果达到 5,您可以停止请求插页式广告一段时间。当应用程序再次启动时,计数器可以被清除。”

我是 java 代码/开发的新手,并且对 SharedPreferences 做了很多研究,但我不知道如何将它合并到我的代码中。

如果有人可以帮助我走上正确的道路或有更好的想法,我会洗耳恭听!

谢谢!

编辑:我的代码:

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.xxxx.xxxxx.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class menuclass extends Activity{

	private InterstitialAd interstitial;
  
  // setting up counter for the interstitial ad
	private int counter = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
      
              //Layout
		setContentView(R.layout.menulayout);
		
		
		//Prepare the Interstitial Ad
		interstitial = new InterstitialAd(menuclass.this);

		//Insert the Ad Unit ID
		interstitial.setAdUnitId("xxxxxx/xxxxxx");

		//Locate the Banner Ad in xml
		AdView adView = (AdView) this.findViewById(R.id.adView);

		//Request for Ads
		AdRequest adRequest = new AdRequest.Builder()

		//Add a test device to show Test Ads
		.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
		.addTestDevice("xxxxxxxxxxxxx")
		.build ();

		//Load ads into Banner Ads
		adView.loadAd(adRequest);

		//Load ads into Interstitial Ads
		loadInterstitial();
	
		//setting up buttons
				Button cp1 = (Button) findViewById(R.id.menu1);
				Button cp2 = (Button) findViewById(R.id.menu2);
				Button cp3 = (Button) findViewById(R.id.menu3);
				Button cp4 = (Button) findViewById(R.id.menu4);
				Button cp5 = (Button) findViewById(R.id.menu5);
				
				
		// On Click Listeners, all buttons are involved in adding to the counter
				
				cp1.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						startActivity(new Intent(menuclass.this, menu1class.class));
						
						counter++;
						displayInterstitial();
						
					}
				});
				
				cp2.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						startActivity(new Intent(menuclass.this, menu2class.class));
						
						counter++;
						displayInterstitial();
					}
				});
				cp3.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						startActivity(new Intent(menuclass.this, menu3class.class));
						
						counter++;
						displayInterstitial();
					}
				});
				cp4.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						startActivity(new Intent(Cmenuclass.this, menu4class.class));
						
						counter++;
						displayInterstitial();
					}
				});
				cp5.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						startActivity(new Intent(menuclass.this, menu5class.class));
						
						counter++;
						displayInterstitial();
					}
				});
		
	}


// displaying the interstitial
private void displayInterstitial()
{

//sharedpreferences
//Here, SharedPreference is changed to SharedPreferences
  SharedPreferences sp = getSharedPreferences("preference name", MODE_PRIVATE);
  //I will try 1 ad per session
  if(sp.getInt("key", 0) < 1){
     Editor ed = sp.edit();
     ed.putInt("key",(sp.getInt("key", 0) +1));
//Here, ed.commint(): is changed to ed.commit();
     ed.commit();
     //do adds work
   }
//end shared preferences

// If Ads are loaded and counter >= 5 show Interstitial, else show nothing.
if (interstitial.isLoaded() & counter>=5)
{
//Resets the counter
// counter = 0; (don't want counter at this point)
interstitial.show();
loadInterstitial();
}
}

private void loadInterstitial()
{
AdRequest adRequest = new AdRequest.Builder().
addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("xxxxxxxxxxxx")
.build(); 
interstitial.loadAd(adRequest);
}

}

最佳答案

首先调用这个 loadInterstitial(); 在你的oncreate中初始加载你的add

我的示例逻辑也符合以下事实:如果您的按钮至少被点击了 5 次或少于 6 次,您可以在 if 语句中更改它。

我摆脱了第一种方法..

使用共享优先方法

  SharedPreferences sp = getSharedPreferences("preference name", MODE_PRIVATE);
  if(sp.getInt("key", 0) < any number here){
     Editor ed = sp.edit();
     ed.putInt("key",(sp.getInt("key", 0) +1));
     ed.commit();
     //do adds work
   }

当你需要显示的时候调用它们,可能放在一个方法中

EDIT 2 让你的代码发挥作用

先生,您的代码很棒,就这样修改吧

private void displayInterstitial(){

//sharedpreferences
//Here, SharedPreference is changed to SharedPreferences - thanks for changing it
SharedPreferences sp = getSharedPreferences("preference name", MODE_PRIVATE);
//I will try 1 ad per session - when you call show(), it shows one add, okay? so
if(sp.getInt("key", 0) < 5){ // the zero is the default value if it's empty, the if statement checks if your sp integer is less than 5.
    // and continues if it checks true, if not, skip the whole statement, that's the logic here
   Editor ed = sp.edit(); 
   ed.putInt("key",sp.getInt("key", 0) +1);
   //Here, ed.commint(): is changed to ed.commit(); -thanks once more- i looked stupid at first
   ed.commit();
   //do adds work - you should put your add code in this method below "do adds work"
   if (interstitial.isLoaded() && counter < 5){ // that's your counter, the && checks if your first argument is true before it moves on to the 
  //second argument.. if it checks false, all is false & checks 
  // checks both arguments regardless of their outcome
        interstitial.show(); 
        loadInterstitial(); 
   } 
 }
  //end shared preferences, end of if-statement, end of logic code
} // end of method 

if 语句是检查器,如果 if 语句检查为 false,则不会显示任何添加,只要您的 int 小于 5,它就会始终检查为 true

还有先生,创建 getter 和 setter 很容易,getter 和 setter 只是帮助设置和检索变量/对象的一些函数,您可以通过声明变量/对象来创建一个,然后,在你的 eclipse 上转到 Source 然后 generate getters and setters .. 它们将由 eclipse 生成 -(如果你使用 eclipse - 但类似于其他 ides)这很容易,还要注意你最初的方式非常好只需要一点点改变

在您的代码中记录行以了解发生了什么

这应该行得通。汤姆爵士

关于java - 如何限制每次 session 的广告数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28167231/

相关文章:

java - sun.security.validator.ValidatorException 当通过 java 使用 web 服务方法时

java - 来自 wsdl 的动态 Web 服务客户端

java - 如何编写单元测试来涵盖抛出 IOException 的情况?

android - 如何从 .java 文件启动画面访问 Monodroid 资源

android - 如何检查蓝牙是否以编程方式启用?

java - 直到我滑动 ViewPager 标题才会出现

android - 如何在 WebView Android 应用程序中禁用 Google Adsense?

java - Spring RabbitMQ SimpleRabbitListenerContainerFactory 用法

android - 用户点击后可以隐藏 admob 横幅吗?

java - Android 应用程序显示广告或应用程序屏幕,但不能同时显示两者