我不想在第一次显示广告横幅,只需在第3次,第6次,第9次加载广告。我能怎么做 ? 如果我希望广告横幅在1分钟后自动关闭,我该怎么办? 如果用户想要关闭广告,有什么办法吗?
这是关注@Nana Ghartey:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
loadTimedAd();
autoCloseAd() ;
}
int adCounter;
private void loadTimedAd() {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(this);
adCounter = sharedPrefs.getInt("prefAdCounter", 0);
if (adCounter % 2 == 0 && connectedToNet())
adshow();
// adView.loadAd(new AdRequest.Builder().build());
else PreferenceManager.getDefaultSharedPreferences(this).edit()
.putInt("prefAdCounter", ++adCounter).commit();
}
private boolean connectedToNet() {
final ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected())
return true;
else
return false;
}
boolean isAdClosed = false;
protected void adshow() {
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.camerar);
RelativeLayout.LayoutParams rlParams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rlParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.ALIGN_PARENT_RIGHT );
final AdView adView = new AdView(this);
adView.setAdUnitId("ca-app-pub-1167514687331347/1312527819");
adView.setAdSize(AdSize.BANNER);
mainLayout.addView(adView, rlParams);
AdRequest adreq = new AdRequest.Builder().build();
adView.loadAd(adreq);
}
private void autoCloseAd(){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(isAdClosed){
//AdView adView = new AdView(this);
adView.setVisibility(View.GONE);
isAdClosed = true;
}
}
}, 3000);
}
答案 0 :(得分:1)
关于第一期,您可以使用计数器在特定时间展示广告。例如,仅在连接到互联网的第3,第6,第9,第12 ...时加载广告:
int adCounter;
private void loadTimedAd() {
SharedPreferences sharedPrefs = PreferenceManager
.getDefaultSharedPreferences(this);
adCounter = sharedPrefs.getInt("prefAdCounter", 0);
if (adCounter % 3 == 0 && connectedToNet())
adView.loadAd(new AdRequest.Builder().build());
else PreferenceManager.getDefaultSharedPreferences(this).edit()
.putInt("prefAdCounter", ++adCounter).commit();
}
//检查互联网是否已连接的方法
private boolean connectedToNet() {
final ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected())
return true;
else
return false;
}
这需要许可:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
关于您的第二个问题,您可以使用横幅广告的可见性标记在一段时间后自动隐藏广告。例如,在一分钟后隐藏广告
boolean isAdClosed = false;
private void autoCloseAd(){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(isAdClosed){
adView.setVisibility(View.GONE);
isAdClosed = true;
}
}
}, 60000);
}
然后最后在你的onCreate()中调用方法
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
adView = (AdView)findViewById(R.id.ad);
loadTimedAd();
autoCloseAd();
}
希望这会有所帮助。如果您需要进一步的帮助,请告诉我