如何在条件不匹配时停止计时器

时间:2017-11-28 09:51:12

标签: android

我写了一个代码来播放视频并在每33毫秒显示每行,但是当条件不匹配时如何停止计时器。

基本上我正在尝试synchronize一次显示的视频和文字

public class Main2Activity extends AppCompatActivity {
    String mf,tf;
    String string;
    TextView tvMf,tvTf,txtLoc;
    Uri uri;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        tvMf = (TextView) findViewById(R.id.v_loc);
        tvTf = (TextView) findViewById(R.id.f_loc);
        Intent intent = getIntent();
        Bundle bd = intent.getExtras();
        if(bd != null)
        {
             mf = (String) bd.get("mediaFile");
             tf =  (String) bd.get("textFile");

           // tvMf.setText(mf);
           // tvTf.setText(tf);
        }
         uri = Uri.parse(mf); //Declare your url here.

        Button playV;
        playV = (Button) findViewById(R.id.play);
        txtLoc =  (TextView) findViewById(R.id.txtLoc);
        playV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                VideoView mVideoView  = (VideoView)findViewById(R.id.videoView);
                mVideoView.setMediaController(new MediaController(Main2Activity.this));
                mVideoView.setVideoURI(uri);
                mVideoView.requestFocus();
                mVideoView.start();
               // Timer timer = new Timer();
                //timer.schedule(new showLoc(), 0, 33);
                try {
                    FileInputStream fis=new FileInputStream(tf);
                    DataInputStream in=new DataInputStream(fis);
                    BufferedReader br=new BufferedReader(new InputStreamReader(in));

                    while ((string = br.readLine()) != null) {
                        final String aFinalString = string;

                        new Timer().schedule(new TimerTask() {
                            @Override
                            public void run() {
                                Main2Activity.this.runOnUiThread(new TimerTask() {
                                    @Override
                                    public void run() {
                                        //Toast.makeText(Main2Activity.this, "str="+string, Toast.LENGTH_SHORT).show();
                                        txtLoc.setText(aFinalString);
                                    }
                                });
                            }
                        }, 0, 33);
                    }

                   if(string==null){

                   }

                    in.close();
                } catch(Exception e){
                    e.printStackTrace();
                }

            }
        });

    }
  }

已编辑的代码

int listSize = loglist.size();
                for (int i = 0; i < listSize; i++) {

                    final int finalI = i;
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            Main2Activity.this.runOnUiThread(new TimerTask() {
                                @Override
                                public void run() {
                                    //Toast.makeText(Main2Activity.this, "str="+string, Toast.LENGTH_SHORT).show();
                                    txtLoc.setText(loglist.get(finalI));
                                }
                            });
                        }
                    }, 0, 33);
                    if(i==listSize){
                        Toast.makeText(Main2Activity.this, "i="+i, Toast.LENGTH_SHORT).show();
                        timer.cancel();
                    }
                }

仍然无法正常工作

0 个答案:

没有答案