如果我有一个在 UI 线程中执行的方法,这个方法会一直运行到最后,还是这个方法可以在中间的某个地方被中断(通过任何将你的活动发送到暂停状态的东西)?
谢谢!
您的方法将因崩溃而中断(例如,未处理的异常)。
它不会被“任何将您的活动发送到暂停状态的东西”中断。
好吧,如果您sleep()
在代码中明确放置了一些地方,这可以将整个 UI 锁定几秒钟。你不能pause a process in android or Java
,你最多只能sleep threads.
如果要在中间退出方法调用,使用“return”关键字。例如:
public void doLengthyCalculation(){
boolean notRequired = true;
//Do something...
//Do something else...
//Now we need to exit
if(notRequired == true){
return; //Anything after this point will not be executed.
}
//Some more code which will not be executed if above if statement evaluates to true
}
希望对您有所帮助。