我正在尝试为我的Android应用程序编写代码,当我按下按钮时,它将在后台运行一个功能,并在我再次按下按钮时退出。有人可以向我解释我是如何做到的。我知道这是围绕线程,但我只是无法弄明白。下面是代码。
public void onMonitorClick(final View view){ //the button
if (isBLEEnabled()) {
if (!isDeviceConnected()) {
// do nothing
} else if (monitorvis == 0) {
showMonitor(); //sets button to display "Stop" and sets monitorvis to 1;
monitorStop = 1;
} else if (monitorvis == 1) {
hideMonitor(); //sets button to display "monitor" and sets monitorvis to 0;
monitorStop = 0;
run(); //this is the thread i want to run in the background, how do i
//make it exit whenever i press onMonitorClick again?
}
}
else {
showBLEDialog();
}
}
protected void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
((ProximityService.ProximityBinder) getService()).getRssi();
rssilevel = ((ProximityService.ProximityBinder) getService()).getRssiValue();
mRSSI.setText(String.valueOf(rssilevel) + "dB");
detectRange(rssilevel);
}
});
}
另外,如何使线程run()延迟,因为它现在正在过快地轮询这些函数。请通过显示代码段或修改我的代码来解释。非常感谢你
现在尝试使用此代码,但没有成功
public void doWork() {
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
@SuppressWarnings("rawtypes")
ScheduledFuture future = service.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
((ProximityService.ProximityBinder) getService()).getRssi();
rssilevel = ((ProximityService.ProximityBinder) getService()).getRssiValue();
mRSSI.setText(String.valueOf(rssilevel) + "dB");
detectRange(rssilevel);
}
},
0, // How long to delay the start
500, // How long between executions
TimeUnit.MILLISECONDS); // The time unit used
}
下次编辑帮助
public void doWork() {
if(mScheduleExecutorService == null) {
mScheduleExecutorService = Executors.newSingleThreadScheduledExecutor();
}
if (mScheduleFuture == null) {
mScheduleFuture = mScheduleExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
do {
((ProximityService.ProximityBinder) getService()).getRssi();
rssilevel = ((ProximityService.ProximityBinder) getService()).getRssiValue();
mRSSI.setText(String.valueOf(rssilevel) + "dB");
detectRange(rssilevel); } while (monitorStop != 3);
}
}, 0, 500, TimeUnit.MILLISECONDS);
}
}
答案 0 :(得分:0)
在这种情况下我会使用ScheduledExecutor。周围有很多例子,但这里有一个快速剪辑:
ScheduleExecutorService service = Executors.newSingleThreadScheduledExecutor();
ScheduleFuture future = service.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// Perform your recurring method calls in here.
}
}
},
0, // How long to delay the start
500, // How long between executions
TimeUnit.MILLISECONDS); // The time unit used
确保在完成后关闭ExecutorService。因此,每当您的应用即将退出时(或者当他们第二次按下按钮时):
service.shutdownNow(); // or service.shutdown();
对于上面的代码段,您需要以下导入:
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
看起来你很近并在路上。我会继续参考未来和服务,以便我可以在其他领域终止,关闭等。尝试以下方面:
private ScheduledExecutorService mScheduleExecutorService;
private ScheduledFuture mScheduleFuture;
public void onMonitorClick(final View view){ //the button
if (isBLEEnabled()) {
if (!isDeviceConnected()) {
// do nothing
} else if (monitorvis == 0) {
showMonitor(); //sets button to display "Stop" and sets monitorvis to 1;
monitorStop = 1;
shutdownExecutor();
} else if (monitorvis == 1) {
hideMonitor(); //sets button to display "monitor" and sets monitorvis to 0;
monitorStop = 0;
doWork(); //this is the thread i want to run in the background, how do i
//make it exit whenever i press onMonitorClick again?
}
}
else {
showBLEDialog();
}
}
public void doWork() {
if(mScheduleExecutorService == null) {
mScheduleExecutorService = Executors.newSingleThreadScheduledExecutor();
}
if (mScheduleFuture == null) {
mScheduleFuture = mScheduleExecutorService.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
((ProximityService.ProximityBinder) getService()).getRssi();
rssilevel = ((ProximityService.ProximityBinder) getService()).getRssiValue();
mRSSI.setText(String.valueOf(rssilevel) + "dB");
detectRange(rssilevel);
}
}, 0, 500, TimeUnit.MILLISECONDS);
}
}
private void shutdownExecutor() {
if (mScheduleExecutorService != null && !mScheduleExecutorService.isShutdown()) {
mScheduleExecutorService.shutdownNow();
}
}