开发者

How to pause the activity?

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() {

            @Overri开发者_运维百科de
            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);
   }
});


A silly solution could be this, which is the way to go in embedded system, although this will surely degrade the performance.

class App extends Activity
{
private static boolean flag = true;

private void stopthread(){
while(flag){

}

}


private void handleUserEvent(){

//do your stuff and 

flag = false;
}
}


The main thread handles UI events, so if you block it (via a sleep()) it ceases to handle the events.

So you can not block the UI thread and expect to unblock it via a user action.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜