我正在开发一个滑雪追踪器应用程序,但我在第一个任务上失败了:)秒表
这是我的服务:
public class TrackerService extends Service {
private IBinder mBinder = new TrackerBinder();
private TimerThread thread;
private int min =0,sec=0;
private boolean running = true;
@Override
public void onCreate() {
thread = new TimerThread();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public void startTimer(){
if(!thread.isAlive())
thread.start();
else{
thread.resume();
}
}
public void pauseTimer(){
thread.suspend();
}
public class TrackerBinder extends Binder{
public TrackerService getService(){
return TrackerService.this;
}
}
public class TimerThread extends Thread{
@Override
public void run() {
while (running){
sec++;
if(sec==60){
min++;
sec=0;
}
try {
Thread.sleep(1000);
}catch(Exception e){}
}
}
}
}
我从绑定(bind)的 Activity 中调用startTimer()和pauseTimer()方法,但在恢复时我得到了java.lang.UnsopportedOperationException。 知道如何解决吗?
最佳答案
线程一旦停止就无法重新启动。但您可以做的是实例化一个新的 TimerThread 并再次启动它!
喜欢
TimerThread newThread = new TimerThread();
newThread.start();
关于java - 线程挂起() UnsupportedOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27628045/