如何在onTouch活动后显示谷歌广告?

时间:2014-05-29 10:25:19

标签: android

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AdRequest adRequest = new AdRequest.Builder().build();
    // adView.loadAd(adRequest);
    interstitial = new InterstitialAd(MainActivity.this);
    interstitial.setAdUnitId("a1536a2f71c5059");
    interstitial.loadAd(adRequest);

    mGLView = new MyGLES10SurfaceView(MainActivity.this);
    setContentView(mGLView);

    final AlertDialog.Builder builder = new AlertDialog.Builder(
            MainActivity.this);
    builder.setTitle("How to use");
    builder.setMessage("1.Crack Screen: touch screen.\n\n2.Exit: Press Back Button 5 times");
    builder.setCancelable(true);
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            AlertDialog alert = builder.create();
            alert.cancel();
            displayInterstitial();

        }
    });

    AlertDialog alert = builder.create();
    alert.show();
}

class MyGLES10SurfaceView extends GLSurfaceView {

    public MyGLES10SurfaceView(Context context) {

        super(context);
        this.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
        this.setRenderer(new MyRenderer(context));
        this.getHolder().setFormat(PixelFormat.TRANSPARENT);

    }

    public boolean onTouchEvent(final MotionEvent event) {

        objPlayer = MediaPlayer.create(MainActivity.this, R.raw.breaks);
        objPlayer.start();
        setContentView(R.layout.main);
         adView = (com.google.android.gms.ads.AdView)
         findViewById(R.id.adView);
         AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);

        return true;
    }

}

public class MyRenderer implements GLSurfaceView.Renderer {

    public MyRenderer(Context contextin) {
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    }

    public void onDrawFrame(GL10 gl) {

        gl.glEnable(GL10.GL_BLEND);
        gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {

    }
}

@Override
public void onBackPressed() {
    if (i == 5) {
        MainActivity.this.finish();
        displayInterstitial();
    }
    i++;
    // do nothing.
}

public void displayInterstitial() {
    if (interstitial.isLoaded()) {
        interstitial.show();
    }
}

我尝试在使用触摸事件后在main.xml布局中显示Google广告。我在onTouch事件方法中放置了广告java代码。首先,我将设置像main.xml的内容,并在触摸后显示广告。

1 个答案:

答案 0 :(得分:0)

您可以将广告视图设置为默认隐藏,并将onTouch方法设置为只显示它。 所以只需将adView.Visible更改为VISIBLE或INVISIBLE。