我正在设计一款应用,用户点击按钮即可显示插页式广告。 关闭广告后,计数器会添加到一个广告,然后显示。 基本上,它是通知用户他们观看了多少广告。 但是计数器没有添加,广告也没有更新。 这是代码: -
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends AppCompatActivity {
private InterstitialAd mInterstitialAd;
private Button watchButton;
int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//TextView tv_data=(TextView)findViewById(R.id.disCount);
MobileAds.initialize(this,
"ca-app-pub-3940256099942544~3347511713");
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() {
mInterstitialAd.loadAd(new AdRequest.Builder().build());
count++;
TextView tv_data=findViewById(R.id.disCount);
setContentView(R.layout.activity_main);
tv_data.setText(Integer.toString(count));
// Load the next interstitial.
}
});
watchButton = findViewById(R.id.button_send);
watchButton.setOnClickListener(new View.OnClickListener() {
// Listen for when user presses button
public void onClick(View v) {
// If a interstitial is ready, show it
if(mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
else
{
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
// Otherwise end this activity (go back to first activity)
}
});
}
}