簡體   English   中英

如何在按鈕上顯示隨機活動單擊在Android Studio中

[英]How Can I Display Random Activity On Button Click In Android Studio

我試圖在廣告加載后顯示隨機活動,但它不顯示按鈕上的隨機活動點擊它只是繼續廣告重新加載到下一個級別。 我希望能夠點擊按鈕,顯示廣告,然后顯示隨機活動,而不是顯示這些級別頁面。 我怎么能這樣做?

package com.ddt.ddts;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Random;


public class MainActivity2Activity extends ActionBarActivity {
// Remove the below line after defining your own ad unit ID.
private static final String TOAST_TEXT = "testing";

private static final int START_LEVEL = 1;
private int mLevel;
private Button mNextLevelButton;
private InterstitialAd mInterstitialAd;
private TextView mLevelTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity2);

    // Create the next level button, which tries to show an interstitial when clicked.
    mNextLevelButton = ((Button) findViewById(R.id.next_level_button));
    mNextLevelButton.setEnabled(false);
    mNextLevelButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showInterstitial();
        }
    });


    // Create the text view to show the level number.
    mLevelTextView = (TextView) findViewById(R.id.next_level_button);
    mLevel = START_LEVEL;

    // Create the InterstitialAd and set the adUnitId (defined in values/strings.xml).
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-123456789/12356");
    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            mNextLevelButton.setEnabled(true);
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            mNextLevelButton.setEnabled(true);
        }

        @Override
        public void onAdClosed() {
            // Proceed to the next level.
            goToNextLevel();
        }
    });
    loadInterstitial();

    }

public void button(View v){
    Random rnd = new Random();
    int x=rnd.nextInt(3)+1;
    Intent intent = new Intent();
    switch(x){
        case 1:
            intent.setClass(this,t1.class);
            break;
        case 2:
            intent.setClass(this,t2.class);
            break;
        case 3:
            intent.setClass(this,t3.class);
            break;
    }
    startActivity(intent);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

private void showInterstitial() {
    // Show the ad if it's ready. Otherwise toast and reload the ad.
    if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
    } else {
        Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
        goToNextLevel();
    }
}

private void loadInterstitial() {
    // Disable the next level button and load the ad.
    mNextLevelButton.setEnabled(false);
    AdRequest adRequest = new AdRequest.Builder().build();
    mInterstitialAd.loadAd(adRequest);
}

private void goToNextLevel() {
    // Show the next level and reload the ad to prepare for the level after.
    mLevelTextView.setText("Level " + (++mLevel));
    loadInterstitial();
}


}

這是您可以隨機顯示插頁式廣告和獎勵視頻的方式:

private void showAd(Intent intent) {
    // every 30 clicks:
    // 5 interstitial ads
    // 1 rewarded videos

    int max_displays = 30;
    int num_interstitial_ads = 5;
    int num_rewarded_videos = 1;

    Random r = new Random();
    int intRandom = r.nextInt(max_displays);
    if ((intRandom >= 0) && (intRandom < num_interstitial_ads)) {
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
            mInterstitialAd.setAdListener(new AdListener() {
                @Override
                public void onAdClosed() {
                    mInterstitialAd.loadAd(new AdRequest.Builder().build());
                    startActivity(intent);
                }
            });
        } else {
            startActivity(intent);
        }
    } else {
        if (intRandom <= num_interstitial_ads + num_rewarded_videos) {
            if (mRewardedVideoAd.isLoaded()) {
                nextIntent = intent;
                mRewardedVideoAd.show();
            } else {
                startActivity(intent);
            }
        } else {
            startActivity(intent);
        }
    }
}


@Override
public void onResume() {
    super.onResume();

    if (!mRewardedVideoAd.isLoaded()) {
        if (AppSettings.DEBUG_MODE)
            mRewardedVideoAd.loadAd(AppSettings.REWARDED_VIDEO_AD_ID_TEST,
                    new AdRequest.Builder().build());
        else
            mRewardedVideoAd.loadAd(AppSettings.REWARDED_VIDEO_AD_ID,
                    new AdRequest.Builder().build());
    }
    if (!mInterstitialAd.isLoaded())
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM