如何暂停活动? [英] How to pause the activity?

查看:184
本文介绍了如何暂停活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想只要用户执行某些操作暂停事件线程/活动(长按为例子),并取消暂停它,当用户执行一些其他操作(简单的点击)。问题是,当我做了很长的点击,事件线程去排队等候。所以,当一个做一个简单的点击,这不是拦截。我该如何实现这一目标?请注意,我不希望显示一些进度对话框等暂停活动,我想保留我的活动前景和获得paused.here是我曾尝试:/

 最后线程t =新主题(新的Runnable(){            @覆盖
            公共无效的run(){
                 同步(本){
                     尝试{
                         this.notify();
                         }赶上(例外五){
                    e.printStackTrace();
                }
                 }
            }
        });
      vg.setOnLongClickListener(新View.OnLongClickListener(){        @覆盖
        公共布尔onLongClick(视图v){
            尝试{
                 同步(本){
                     this.wait();
                     }
            }赶上(InterruptedException的E){
                e.printStackTrace();
            }
            返回true;
        }
    });
      vg.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
             同步(本){
                 t.start();}
        }    });


解决方案

正如其他人所说,的暂停的活动的主UI线程不因各种原因一个好主意。

这听起来像你真正希望做的是禁用各种UI元素,而不是暂停的活动。这可以通过调用的setEnabled(布尔)上禁用/启用的各种元素来完成。

当用户点击暂停按钮,你可以通过所有的UI元素和禁用它们(除了取消暂停按钮)。然后,当用户点击取消暂停按钮,重新启用所有禁用的元素。

编辑:
如果我理解你在找什么,这是一种方法,暂停和取消暂停工作线程。工作线程将在后台执行DOA()和DOB(),并可以在每次的它们之间被暂停。您可以添加更多的功能,甚至取消使用此方法。

 
布尔mIsPaused = FALSE;最终线程的WorkerThread =新主题(新的Runnable(){
   @覆盖
   公共无效的run(){
      DOA();
      checkPause();
      DOB();
      checkPause();
      ...
   }
   }
});私人无效checkPause(){
   而(isPaused得到()){
      //你也可以使用的通知/等待模式,但认为可能是不必要的这种使用情况的复杂性。
      视频下载(50);
   }
}私人同步布尔isPaused得到(){
  返回mIsPaused;
}私人同步无效setPaused(布尔isPaused得到){
  mIsPaused = isPaused得到;
}
pauseButton.setOnLongClickListener(新View.OnLongClickListener(){
   @覆盖
   公共布尔onLongClick(视图v){
      //禁用任何需要它的用户界面元素
      setIsPaused(真);
   }
});unPauseButton.setOnLongClickListener(新View.OnLongClickListener(){
   @覆盖
   公共布尔onLongClick(视图v){
      //重新启用任何需要它的用户界面元素
      setIsPaused(假);
   }
});

I want to pause the "Event Thread"/ activity as soon as the user does some action(long click for example), and unpause it when user does some other action(simple click). The problem is, after i do a long click, The event thread goes in the waiting queue. SO when a do a simple click, that is not intercepted. How do I achieve this? please note, i do not want to pause the activity by showing some progress dialog etc., i want to retain my activity in foreground and get paused.here is what i have tried :/

 final Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                 synchronized(this){    
                     try {
                         this.notify();
                         } catch (Exception e) {
                    e.printStackTrace();
                }
                 }
            }
        });
      vg.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            try {
                 synchronized(this){    
                     this.wait();
                     }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return true;
        }
    });
      vg.setOnClickListener(new View.OnClickListener() {


        @Override
        public  void onClick(View v) {
             synchronized(this){    
                 t.start();}
        }

    });

解决方案

As others have said, pausing the activity's main UI thread is not a good idea for various reasons.

It sounds like what you're really looking to do is disable various UI elements instead of pausing the activity. This can be done by calling setEnabled(boolean) on the various elements to be disabled/enabled.

When the user clicks the 'pause' button you can go through all the UI elements and disable them (except for the 'unpause' button). Then when the user clicks the 'unpause' button, you re-enable all the disabled elements.

Edit: If I understand what you're looking for, this is a way to pause and unpause a worker thread. The worker thread will execute doA() and doB() in the background and can be paused between each of these. You can add more functions and even a cancel using this method.


boolean mIsPaused = false;

final Thread workerThread = new Thread(new Runnable() {
   @Override
   public void run() {
      doA();
      checkPause();
      doB();
      checkPause();
      ...
   }
   }
});

private void checkPause() {
   while(isPaused()) {
      // you could also use the notify/wait pattern but that is probably needless complexity for this use case.
      Thread.sleep(50);
   }
}

private synchronized boolean isPaused() {
  return mIsPaused;
}

private synchronized void setPaused(boolean isPaused) {
  mIsPaused = isPaused;
}


pauseButton.setOnLongClickListener(new View.OnLongClickListener() {
   @Override
   public boolean onLongClick(View v) {
      // disable any UI elements that need it
      setIsPaused(true);
   }
});

unPauseButton.setOnLongClickListener(new View.OnLongClickListener() {
   @Override
   public boolean onLongClick(View v) {
      // re-enable any UI elements that need it
      setIsPaused(false);
   }
});

这篇关于如何暂停活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆