循环线程- 为什么要使用循环线程,因为线程只执行一次,因为一直运行线程中的代码,那么线程会一直存在,直到某个值的变化!
private boolean isplay;
isplay = true;
dialog = ProgressDialog.show(this, null, "正在加载…", true, true);
dialog.setCancelable(false);
// 获得线程消息 final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { String s = (String) msg.obj; if ("videook".equals(s)) { // dialog.dismiss(); dialog.dismiss(); isplay = false; //检测到视频播放了,那么就把dialog.dismiss掉,并且要结束掉线程循环 } else if ("no".equals(s)) { Toast.makeText(getApplicationContext(), "系统问题", 1).show(); } } }; try { Intent i = getIntent(); uri = i.getStringExtra("videoPath");// "android.resource://" + // getPackageName() + "/" + // R.raw.yqs; integral = i.getStringExtra("integral");// "android.resource://" + // getPackageName() + "/" + // R.raw.yqs; vid = i.getIntExtra("vid", 0);// "android.resource://" + // getPackageName() + "/" + // R.raw.yqs; video1.setVideoURI(Uri.parse(uri)); video1.start(); // 循环线程 ---播放线程,要一直判断视频是否在播放 new Thread() { public void run() { while (true == isplay) { if(video1.isPlaying()){ Message msg = new Message(); msg.obj = "videook"; handler.sendMessage(msg); } } }; }.start(); video1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // 播放结束后的动作 AppManager.getAppManager().finishActivity(); addIntengral(); sendReq(); } }); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }