我正在尝试向其显示一个按钮,以显示AdMob插页式广告,并在用户关闭后将其带到下一个活动。但是,我希望在插页式广告加载时按钮可见,直到那时,我要显示一个TextView,上面写着“应用程序正在加载...”。
这是我到目前为止创建的。
Button goFree;
private TextView loadingFree;
private InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ask);
Button goFree = (Button) findViewById(R.id.goFree);
loadingFree = findViewById(R.id.loadingFeee);
MobileAds.initialize(RegisterNote.this, "cca-app-pub-3940256099942544/1033173712");
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
startActivity(new Intent(Ask.this, Free.class));
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});
if (mInterstitialAd.isLoaded()) {
goFree.setVisibility(View.VISIBLE);
lodingFree.setVisibility(View.GONE);
} else {
goFree.setVisibility(View.GONE);
lodingFree.setVisibility(View.VISIBLE);
}
goFree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mInterstitialAd.show();
}
});
}
但是问题是,即使在广告加载时,TextView的可见性仍为VISIBLE
,按钮仍为GONE
。可能是因为在广告加载时,它没有调用该函数来更改可见性。
我应该在这里做什么?仅在广告加载后如何显示按钮?
答案 0 :(得分:1)
尝试像使用onAdLoaded()
一样使用onAdClosed()
函数,代码应与此类似:
goFree.setVisibility(View.GONE);
lodingFree.setVisibility(View.VISIBLE);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
goFree.setVisibility(View.VISIBLE);
lodingFree.setVisibility(View.GONE);
}
@Override
public void onAdClosed() {
startActivity(new Intent(Ask.this, Free.class));
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});
然后您可以删除支票if else
。
来源:https://developers.google.com/android/reference/com/google/android/gms/ads/InterstitialAd
希望这会有所帮助
答案 1 :(得分:1)
AdListener中的每个可覆盖方法对应于广告生命周期中的一个事件。
加载加载项时,您可以在onAdLoaded()方法中编写代码,这将隐藏textview和可见按钮。
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
goFree.setVisibility(View.VISIBLE);
lodingFree.setVisibility(View.GONE);
}
@Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}
@Override
public void onAdOpened() {
// Code to be executed when the ad is displayed.
}
@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
@Override
public void onAdClosed() {
// Code to be executed when when the interstitial ad is closed.
}
答案 2 :(得分:0)
Check the below example that i have implemented. Hope it will help you. Just check this method
holder.cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// showInterstitial();
if (interstitialAd != null && interstitialAd.isLoaded()) {
interstitialAd.show();
} else {
Intent intent = new Intent();
Bundle bundle =
ActivityOptions.makeSceneTransitionAnimation((Activity) mContext,
holder.thumbnailImage, holder.thumbnailImage.getTransitionName()).toBundle();
intent.setClass(mContext, AboutMovieActivity.class);
intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_ID,
mSearchedMovies.get(position).getId());
intent.putExtra(IntentConstants.INTENT_KEY_POSTER_PATH,
mSearchedMovies.get(position).getPosterPath());
intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_NAME,
mSearchedMovies.get(position).getTitle());
mContext.startActivity(intent, bundle);
startGame();
}
interstitialAd.setAdListener(new AdListener(){
@Override
public void onAdClosed() {
Intent intent = new Intent();
Bundle bundle =
ActivityOptions.makeSceneTransitionAnimation((Activity) mContext,
holder.thumbnailImage, holder.thumbnailImage.getTransitionName()).toBundle();
intent.setClass(mContext, AboutMovieActivity.class);
intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_ID,
mSearchedMovies.get(position).getId());
intent.putExtra(IntentConstants.INTENT_KEY_POSTER_PATH,
mSearchedMovies.get(position).getPosterPath());
intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_NAME,
mSearchedMovies.get(position).getTitle());
mContext.startActivity(intent, bundle);
}
});
Full code is below:-
public class MoviesSearchAdapter extends
RecyclerView.Adapter<MoviesSearchAdapter.ViewHolder> {
Context mContext;
private ArrayList<Movie> mSearchedMovies;
private InterstitialAd interstitialAd;
public MoviesSearchAdapter(ArrayList<Movie> searchedMovies, Context context) {
mContext = context;
mSearchedMovies = searchedMovies;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v =
LayoutInflater.from(parent.getContext()).inflate
(R.layout.search_movie_list_item,
parent, false);
// Initialize the Mobile Ads SDK. ca-app-pub-7152815429504851~7360705844
MobileAds.initialize(mContext, "ca-app-pub-7152815429504851~8085885141");
// Create the InterstitialAd and set the adUnitId.
interstitialAd = new InterstitialAd(mContext);
// Defined in res/values/strings.xml ca-app-pub-
7152815429504851/2822695030
interstitialAd.setAdUnitId("ca-app-pub-7152815429504851/4913761040");
startGame();
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
if (mSearchedMovies != null) {
holder.name.setText(mSearchedMovies.get(position).getTitle());
Picasso.with(mContext).load(URLConstants.IMAGE_BASE_URL +
mSearchedMovies.get(position).getPosterPath()).into(holder.thumbnailImage);
if (mSearchedMovies.get(position).getDate().length() >= 5) {
String date = mSearchedMovies.get(position).getDate().substring(0,
4);
holder.releaseDate.setText(date);
}
String rating =
Double.toString(mSearchedMovies.get(position).getRating());
holder.rating.setText(rating);
holder.cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// showInterstitial();
if (interstitialAd != null && interstitialAd.isLoaded()) {
interstitialAd.show();
} else {
Intent intent = new Intent();
Bundle bundle =
ActivityOptions.makeSceneTransitionAnimation((Activity) mContext,
holder.thumbnailImage, holder.thumbnailImage.getTransitionName()).toBundle();
intent.setClass(mContext, AboutMovieActivity.class);
intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_ID,
mSearchedMovies.get(position).getId());
intent.putExtra(IntentConstants.INTENT_KEY_POSTER_PATH,
mSearchedMovies.get(position).getPosterPath());
intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_NAME,
mSearchedMovies.get(position).getTitle());
mContext.startActivity(intent, bundle);
startGame();
}
interstitialAd.setAdListener(new AdListener(){
@Override
public void onAdClosed() {
Intent intent = new Intent();
Bundle bundle =
ActivityOptions.makeSceneTransitionAnimation((Activity) mContext,
holder.thumbnailImage, holder.thumbnailImage.getTransitionName()).toBundle();
intent.setClass(mContext, AboutMovieActivity.class);
intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_ID,
mSearchedMovies.get(position).getId());
intent.putExtra(IntentConstants.INTENT_KEY_POSTER_PATH,
mSearchedMovies.get(position).getPosterPath());
intent.putExtra(IntentConstants.INTENT_KEY_MOVIE_NAME,
mSearchedMovies.get(position).getTitle());
mContext.startActivity(intent, bundle);
}
});
}
});
}
}
@Override
public int getItemCount() {
return mSearchedMovies.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
CardView cv;
ImageView thumbnailImage;
TextView name;
TextView releaseDate;
TextView rating;
public ViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cardView);
thumbnailImage = (ImageView)
itemView.findViewById(R.id.thumbnailImageView);
name = (TextView) itemView.findViewById(R.id.nameTextView);
releaseDate = (TextView)
itemView.findViewById(R.id.releaseDateTextView);
rating = (TextView) itemView.findViewById(R.id.ratingTextView);
}
}
private void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and restart the game.
if (interstitialAd != null && interstitialAd.isLoaded()) {
interstitialAd.show();
} else {
//Toast.makeText(this, "Your Internet is not Working",
Toast.LENGTH_SHORT).show();
startGame();
}
}
private void startGame() {
// Request a new ad if one isn't already loaded, hide the button, and kick
off the timer.
if (!interstitialAd.isLoading() && !interstitialAd.isLoaded()) {
AdRequest adRequest = new AdRequest.Builder().build();
interstitialAd.loadAd(adRequest);
}
// retryButton.setVisibility(View.INVISIBLE);
}
}
答案 3 :(得分:0)
您好Hijibiji尝试使用此代码,它将对您有所帮助。
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
goFree.setVisibility(View.VISIBLE);
lodingFree.setVisibility(View.GONE);
}
@Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}
@Override
public void onAdOpened() {
// Code to be executed when the ad is displayed.
}
@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
@Override
public void onAdClosed() {
// Code to be executed when when the interstitial ad is closed.
}
});
引用this链接