开发者

Admob in android. How to make it appear and disappear

开发者 https://www.devze.com 2023-01-22 20:55 出处:网络
I am new to programming in android. I have followed instruction and create a admob banner.开发者_如何学C How can I make it appear at certain intervals, and make it disappear if I want to? For example

I am new to programming in android. I have followed instruction and create a admob banner.开发者_如何学C 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 );//*/
    }
}


  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) {
                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
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消