tags:

views:

67

answers:

2

I am new to programming in android. I have followed instruction and create a admob banner. How can I make it appear at certain intervals, and make it disappear if I want to? For example the admob banner can goes up and down at the bottom of the screen whenever it wants to. Thanks.

Edit:

I know that i can call adView.setVisibility( View.GONE ); to make the ads appear and disappear, but when i try to write it into a thread to make it appear and disappear with intervals, it just hangs there with a black screen.

Or is there anyway that admob can make their ad appear and disappear at intervals?

This is how i call the thread.

loadAdmob = new asyncAdmobProc();
loadAdmob.execute();
loadAdmob.doInBackground();//asyncAdmobProc();

The code:

//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 );//*/
    }
}
A: 

See question edit.

A: 
  1. You don't have to call AsyncTask.doInBackground, this method will be called by AsyncTask itself.
  2. AsyncTask.doInBackground is called in other thread instead of UI thread, you may not want to start an animation in it, that will cause some problems on UI.
  3. There are lots of ways that you can make the view appear and disappear with intervals, using AsyncTask is not one of them, I think. Following is sample code which use Handler to archive this.
    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) {
                switch (msg.what) {
                case SHOW:
                    adView.setVisibility(View.VISIBLE);
                case HIDE:
                    adView.setVisibility(View.GONE);
                }
            }
        }

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

        // Other methods
    }
Tony