暂停和恢复活动中的可运行线程

时间:2017-08-01 08:24:02

标签: java android multithreading runnable

我正在制作一款培训应用。在我的应用程序中,一个视频正在播放一些第二个警报对话框后显示对话框包含问题和答案学生阅读问题并选择答案,直到我的线程想要等待。在这里,我成功地获得了视频计时。我想暂停线程直到关闭警报对话框。

                mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    public void onPrepared(MediaPlayer mp) {
                        running = true;
                        final int duration = mVideoView.getDuration();

                        new Thread(new Runnable() {
                            boolean myRunning;

                            public void run() {
                                synchronized (mSync) {
                                    myRunning = mSync.running;
                                }
                                do {
                                    textView.post(new Runnable() {
                                        public void run() {
                                            int time = Math.round(mVideoView.getCurrentPosition() / 1000);
                                            textView.setText(time + "");
                                            if (time == 5) {
                                                running = false;
                                                mVideoView.pause();
                                                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Page02.this);
                                                alertDialogBuilder.setMessage("Are you sure,You wanted to make decision");
                                                alertDialogBuilder.setPositiveButton("yes",
                                                        new DialogInterface.OnClickListener() {
                                                            @Override
                                                            public void onClick(DialogInterface arg0, int arg1) {
                                                                onResume();
                                                                mVideoView.start();
                                                                arg0.dismiss();
                                                            }
                                                        });

                                                alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                                                    @Override
                                                    public void onClick(DialogInterface dialog, int which) {
                                                        finish();
                                                    }
                                                });
                                                AlertDialog alertDialog = alertDialogBuilder.create();
                                                alertDialog.show();
                                            }
                                        }
                                    });
                                    try {
                                        Thread.sleep(500);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    if (!running) break;
                                }
                                while (mVideoView.getCurrentPosition() < duration);
                            }
                        }).start();
                    }
                });
            }

            @Override
            protected void onPause() {
                super.onPause();
                synchronized (mSync) {
                    running = false;// you can not set it here because
                    // it is possible for the thread to read it and exit the loop before he posts your message
                    mSync.mustBePost = true;
                    mSync.message = "Main Activity is going to pause";
                }
            }

            @Override
            protected void onResume() {
                super.onResume();
                threadNameCounter++;
                synchronized (mSync) {
                    running = true;
                    mSync.mustBePost = true;
                    mSync.message = "Main Activity is going to resume";
                }
                t = new Thread("My Name is " + String.valueOf(threadNameCounter));
                t.start();
            }
        }

0 个答案:

没有答案