在Android中的Admob.如何让它出现和消失

use*_*146 1 android admob

我是android新手编程的新手.我遵循了指示并创建了一个admob横幅.如何让它以特定的间隔出现,如果我愿意,它会消失?例如,admob横幅可以随时在屏幕底部上下移动.谢谢.

编辑:

我知道我可以打电话adView.setVisibility( View.GONE );让广告显示和消失,但当我尝试将其写入一个线程使其显示并消失时间间隔时,它只是挂在那里黑屏.

或者,无论如何,admob可以让他们的广告每隔一段时间出现和消失?

这就是我调用线程的方式.

loadAdmob = new asyncAdmobProc();
loadAdmob.execute();
loadAdmob.doInBackground();//asyncAdmobProc();
Run Code Online (Sandbox Code Playgroud)

代码:

//wakes up the admob
private class asyncAdmobProc extends AsyncTask<Integer , Void, Integer> {

    private boolean bconthread=true;
    protected Integer doInBackground(Integer... Params) {
        //wakes up and disable admob

        /*AdManager.setTestDevices( new String[] {
                AdManager.TEST_EMULATOR, // Android emulator
                "E83D20734F72FB3108F104ABC0FFC738", // My T-Mobile G1 Test Phone
                } );//*/

        adView = (AdView)findViewById(R.id.articleList_ads);
        adView.requestFreshAd();
        adView.setVisibility( View.GONE );

        //while(bconthread){
            adView.requestFreshAd();
            ShowAd();

            postDelayed();

            //HideAd();

            postDelayed();

        //}

        //call this to delete all bitmaps associated with the ad
        adView.cleanup();
        return 0;
    }
    private void HideAd()
    {
        // Hide the ad.
        adView.setVisibility( View.GONE );

        // Fade the ad in over 4/10 of a second.
        AlphaAnimation animation = new AlphaAnimation( 0.0f, 1.0f );
        animation.setDuration( 400 );
        animation.setFillAfter( true );
        animation.setInterpolator( new AccelerateInterpolator() );
        adView.startAnimation( animation );//*/

    }
    private void ShowAd()
    {
        // Unhide the ad.
        adView.setVisibility( View.VISIBLE );

        // Fade the ad in over 4/10 of a second.
        AlphaAnimation animation = new AlphaAnimation( 0.0f, 1.0f );
        animation.setDuration( 400 );
        animation.setFillAfter( true );
        animation.setInterpolator( new AccelerateInterpolator() );
        adView.startAnimation( animation );//*/
    }
}
Run Code Online (Sandbox Code Playgroud)

Ton*_*ony 6

  1. 您不必调用AsyncTask.doInBackground,此方法将由AsyncTask本身调用.
  2. AsyncTask.doInBackground 在其他线程而不是UI线程中调用,您可能不想在其中启动动画,这将导致UI上的一些问题.
  3. 我认为有很多方法可以使视图显示和消失,间隔使用AsyncTask不是其中之一.以下是使用Handler存档的示例代码.
    public MyActivity extends Activity {

        private static final int SHOW = 1;
        private static final int HIDE = -1;

        private View adView;

        private Handler handler = new Handler() {
            public void handleMessage(Message msg) {
                adView.setVisibility(msg.what);                
            }
        }

        private void startTriggerThread() {
            new Thread() {
                boolean show = false;
                public void run() {
                    while (true) {
                        if (show) {
                            handler.sendEmptyMessage(View.GONE);
                        } else {
                            handler.sendEmptyMessage(View.VISIBLE);
                        }
                        show = !show;
                        try {
                            Thread.sleep(INTERVALS);
                        }
                        catch (InterruptException e) {
                            // Ignore.
                        }
                    }
                }
            }.start();
        }

        // Other methods
    }
Run Code Online (Sandbox Code Playgroud)