我是android编程的新手。我已按照说明进行操作,并创建了一个admob标语。我怎样才能使它每隔一定的时间出现一次,并使其消失呢?例如,admob标语可以随时在屏幕底部上下移动。
谢谢。
编辑:
我知道我可以调用adView.setVisibility( View.GONE );
来使广告显示和消失,但是当我尝试将其写入线程以使其间隔出现和消失时,它只是挂在黑屏上。
还是反而有admob可以使其广告定期显示和消失?
这就是我所说的线程。
loadAdmob = new asyncAdmobProc();
loadAdmob.execute();
loadAdmob.doInBackground();//asyncAdmobProc();
代码:
//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 );//*/
}
}
最佳答案
您不必调用AsyncTask.doInBackground
,此方法将由AsyncTask本身调用。
在其他线程而不是UI线程中调用AsyncTask.doInBackground
,您可能不想在其中启动动画,这会在UI上引起一些问题。
我认为有很多方法可以使视图间隔显示和消失,使用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
}