我是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 );//*/
}
}
答案 0 :(得分:6)
AsyncTask.doInBackground
,AsyncTask本身将调用此方法。 AsyncTask.doInBackground
在其他线程而不是UI线程中调用,您可能不想在其中启动动画,这会在UI上引起一些问题。 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
}