Android 中的线程。帮助?

发布于 2024-12-18 18:13:56 字数 1509 浏览 0 评论 0原文

我正在尝试为手电筒应用程序编写一个新功能。我很确定我需要一个新线程来执行此操作。但是,我是线程新手,根本不知道该怎么做。我想做的是改变 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十级心震 2024-12-25 18:13:56

我不确定是否需要在每次进度发生变化时创建一个新线程,更好的方法可能是创建一个最终的可运行对象,在其中更改整数值:

onProgressChange:

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
        toRun.interval = progress;
    }

A runnable-implementation:

    class StrobeRunnable implements Runnable {
        volatile boolean running;
        volatile int interval = 0;
        private boolean lightIsOn = false;

        @Override
        public void run() {
            while(running){
                if (interval > 1){
                    Thread.sleep(300/interval);
                    lightIsOn = !lightIsOn;
                    switchLight(lightIsOn);
                }
                else{
                    Thread.sleep(600/interval);
                    if( !lightIsOn ){
                        lightIsOn = true;
                        switchLight(lightIsOn);
                    }
                }
            }
        }
    };

请注意,我为某些对象创建了新名称方法,
switchLight(boolean isLightOn) 是我相信您的 processXClick 之前所做的,即打开或关闭灯。
间隔以前称为 myInt。

要启动线程,我更喜欢使用 Executors:

    StrobeRunnable toRun = new StrobeRunnable();
    Executor exec = Executors.newSingleThreadExecutor();
    exec.execute(toRun);

要完全停止线程运行,请将 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:

    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
        toRun.interval = progress;
    }

A runnable-implementation:

    class StrobeRunnable implements Runnable {
        volatile boolean running;
        volatile int interval = 0;
        private boolean lightIsOn = false;

        @Override
        public void run() {
            while(running){
                if (interval > 1){
                    Thread.sleep(300/interval);
                    lightIsOn = !lightIsOn;
                    switchLight(lightIsOn);
                }
                else{
                    Thread.sleep(600/interval);
                    if( !lightIsOn ){
                        lightIsOn = true;
                        switchLight(lightIsOn);
                    }
                }
            }
        }
    };

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:

    StrobeRunnable toRun = new StrobeRunnable();
    Executor exec = Executors.newSingleThreadExecutor();
    exec.execute(toRun);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文