Android 中的线程。帮助?
我正在尝试为手电筒应用程序编写一个新功能。我很确定我需要一个新线程来执行此操作。但是,我是线程新手,根本不知道该怎么做。我想做的是改变 SeekBar,我希望频闪变得更快/更慢。如果为 0 或 1,则恢复常亮。
这是错误的,但这就是我所拥有的。每当我移动 SeekBar 时,它都会正确地闪烁,但你不能让它停止。
在 onCreate():
mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
final int myInt = progress;
new Thread (new Runnable() {
public void run() {
if (myInt>1)
strobeLight(myInt);
else {}
// Stop Strobe
} }).start();
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onStopTrackingTouch(SeekBar seekBar) {}
});
strobeLight() 方法中:
public void strobeLight(int myInt){
do {
if (myInt>1){
if (strobe){
processOffClick();
try { Thread.sleep(300/myInt); }
catch (InterruptedException e) {}
strobe=false;
strobeActivated=true;}
else{
processOnClick();
try { Thread.sleep(300/myInt); }
catch (InterruptedException e) {}
strobe=true; }
}
else{
processOnClick();
strobe=false;
Thread.currentThread().interrupt();
strobeActivated=false;}
} while (strobeActivated);
}
I am trying to write a new feature for a flashlight app. I am pretty sure I need a new Thread to do this. HOWEVER, I am new to Threads and do not know how to do it at all. What I am trying to do is on the change of a SeekBar, I want the strobe to get faster/ slower. If it is 0 or 1 resume to constant light.
This is wrong, but this is what I have. Whenever I move the SeekBar it correctly strobes, but you cannot make it stop..
In onCreate():
mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
final int myInt = progress;
new Thread (new Runnable() {
public void run() {
if (myInt>1)
strobeLight(myInt);
else {}
// Stop Strobe
} }).start();
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onStopTrackingTouch(SeekBar seekBar) {}
});
strobeLight() method:
public void strobeLight(int myInt){
do {
if (myInt>1){
if (strobe){
processOffClick();
try { Thread.sleep(300/myInt); }
catch (InterruptedException e) {}
strobe=false;
strobeActivated=true;}
else{
processOnClick();
try { Thread.sleep(300/myInt); }
catch (InterruptedException e) {}
strobe=true; }
}
else{
processOnClick();
strobe=false;
Thread.currentThread().interrupt();
strobeActivated=false;}
} while (strobeActivated);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定是否需要在每次进度发生变化时创建一个新线程,更好的方法可能是创建一个最终的可运行对象,在其中更改整数值:
onProgressChange:
A runnable-implementation:
请注意,我为某些对象创建了新名称方法,
switchLight(boolean isLightOn) 是我相信您的 processXClick 之前所做的,即打开或关闭灯。
间隔以前称为 myInt。
要启动线程,我更喜欢使用 Executors:
要完全停止线程运行,请将 toRun.running 设置为 false,这将停止运行循环。
我还没有测试过这个,但它应该可以工作。它还有点取决于您的 processClick 方法的外观。
I am unsure about the need of creating a new thread every time the progress changes, a better approach might be to create a final runnable in which you change the integer value:
onProgressChange:
A runnable-implementation:
Note that I created new names for some methods,
switchLight(boolean isLightOn) is what I believe your processXClick did before, namely switch the light on or of.
And interval used to be called myInt.
To start a thread I prefer to use Executors:
To completely stop the thread from running, set the toRun.running to false, and this will stop the running loop.
I haven't tested this but it should work. It also depends a bit on how your processClick-method looks.