int r = 2;
开始循环
thread t = new thread(); Thread c = new Control(t); t.setSleep(true); c.setDaemon(true); t.start(); c.start();
/**
* 循环上报司机信息
*/
class thread extends Thread {
private boolean isSleep = true;
private boolean isStop = false;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void run() {
while (!isStop) {
if (isSleep) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
Log.e("run",CustomizeUtil.getEquipmentPower(mContext.getApplicationContext())+"/"+CustomizeUtil.getSystemStartupTime()+"/"
+CustomizeUtil.StringgetAppVersionName(mContext.getApplicationContext())
+"/"+LocationUtils.getInstance().getLocations(mContext.getApplicationContext()).toString()
);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("Thread: " + Thread.currentThread().getName() + " 结束.");
}
public void setSleep(boolean sleep) {
this.isSleep = sleep;
}
public void setStop(boolean stop) {
this.isStop = stop;
}
}
class Control extends Thread {
private thread t;
public Control(thread t) {
this.t = t;
}
public void run() {
while (true) {
if (r == 0) { //r=0运行
t.setSleep(false);
} else if (r == 1) { //r=1暂停
t.setSleep(true);
} else if (r == 2) { //r=2 停止
t.setStop(true);
}
}
}
}
在onclick 事件中修改 r的值就可以完美实现