public class LogoActivity extends BaseActivity implements Runnable {
int num = 0;
boolean flag = true;
Bitmap[] bitmaps;
ImageView image;
private boolean suspendFlag = false;// 控制线程的执行状态
String[] url = { "http://www.10010.com/goodsdetail/511405139289.html",
"http://www.10010.com/tesla",
"http://www.10010.com/goodsdetail/981410108357.html" };
int logoId[] = { R.drawable.logo1, R.drawable.logo2, R.drawable.logo3 };
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
image.setBackgroundResource(logoId[num]);
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.logo);
Resources res = getResources();
bitmaps = new Bitmap[3];
for (int i = 0; i < bitmaps.length; i++) {
bitmaps[i] = BitmapFactory.decodeResource(res, logoId[i]);
}
image = (ImageView) this.findViewById(R.id.logo);
Thread thread = new Thread(this);
thread.start();
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
link(url[num]);
}
});
}
private void link(String str) { //调用浏览器加载广告页
// TODO Auto-generated method stub
String adress = str; // web address
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(adress));
startActivity(intent);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
suspend();
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
resume();
super.onResume();
}
public void suspend() { //线程暂停
this.suspendFlag = true;
}
public synchronized void resume() { //唤醒线程
this.suspendFlag = false;
this.notify();
}
public void run() {
// TODO Auto-generated method stub
while (flag) {
try {
Thread.sleep(2000L);
synchronized (this) {
//同步代码块控制线程状态
while (suspendFlag) {
wait();
}
}
if (num < bitmaps.length - 1) {
num++;
handler.sendEmptyMessage(1);
} else {
flag = false;
Intent it = new Intent(LogoActivity.this,
WelcomeActivity.class);
startActivity(it);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
finish();
}
}