Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何切换手电筒模式?_Java_Android_Flashlight - Fatal编程技术网

Java 如何切换手电筒模式?

Java 如何切换手电筒模式?,java,android,flashlight,Java,Android,Flashlight,我想在我的应用程序中添加一个按钮(我已将其包含在我的activity_main中),当按下该按钮时,该按钮将启动选通效果,当再次按下该按钮时,该按钮将停止。我不在乎速度,只是它反复切换闪光模式和闪光模式,直到再次按下按钮 我试过: 使用处理程序 创建单独的类 包含处理程序的单独类不起作用,因为在主活动中没有启动的意图,或者在清单中没有启动的意图,因为它的代码没有完成,因为我想在这里询问它是如何以最简单的方式完成的 StrobeLightConfig.java import android.a

我想在我的应用程序中添加一个按钮(我已将其包含在我的activity_main中),当按下该按钮时,该按钮将启动选通效果,当再次按下该按钮时,该按钮将停止。我不在乎速度,只是它反复切换闪光模式和闪光模式,直到再次按下按钮

我试过:

  • 使用处理程序
  • 创建单独的类

包含处理程序的单独类不起作用,因为在主活动中没有启动的意图,或者在清单中没有启动的意图,因为它的代码没有完成,因为我想在这里询问它是如何以最简单的方式完成的

StrobeLightConfig.java

import android.app.Activity;
    import android.content.Intent;
    import android.hardware.Camera;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.widget.ImageButton;
    import android.widget.SeekBar;
    import android.widget.SeekBar.OnSeekBarChangeListener;

    public class StrobeLightConfig extends Activity {

        boolean check = false;

        Camera sandy;
        StrobeRunner runner;
        Thread bw;
        ImageButton btnClick;

        public final Handler mHandler = new Handler();

        public final Runnable mShowToastRunnable = new Runnable() {
            public void run() {

            }
        };

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            btnClick = (ImageButton) findViewById(R.id.btnSwitch);


            runner = StrobeRunner.getInstance();
            runner.controller = this;

            if (runner.isRunning) {

            } else {
                try {
                    sandy = Camera.open();

                    if (sandy == null) {
                        return;
                    }

                    sandy.release();
                } catch (RuntimeException ex) {
                    return;
                }
            }

            bw = new Thread(runner);
            bw.start();

            btnClick.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    if (check) {
                        bw = new Thread(runner);
                        bw.start();
                        check = false;
                    } else {
                        check = true;
                        runner.requestStop = true;
                    }            
                }
            });

            final SeekBar skbar = (SeekBar) findViewById(R.id.SeekBar01);
            skbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                        boolean fromUser) {


                    runner.delay = 101 - progress;
                    runner.delayoff = 101 - progress;

                }
            });

        }

        @Override
        protected void onStop() {
    //        runner.requestStop = true;

            super.onStop();
        }

        @Override
        public void onBackPressed() {
            // TODO Auto-generated method stub
    //        super.onBackPressed();

            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startActivity(startMain);

        }

    }
StrobeRunner.java

    import android.hardware.Camera;

public class StrobeRunner implements Runnable {

    protected StrobeRunner()
    {

    }

    public static StrobeRunner getInstance()
    {
        return ( instance == null ? instance = new StrobeRunner() : instance );
    }

    private static StrobeRunner instance;


    public volatile boolean requestStop = false;
    public volatile boolean isRunning = false;
    public volatile int delay = 10;
    public volatile int delayoff = 500;
    public volatile StrobeLightConfig controller;
    public volatile String errorMessage = "";

    @Override
    public void run() {
        if(isRunning)
            return;

        requestStop=false;
        isRunning = true;

        Camera cam = Camera.open();

        Camera.Parameters pon = cam.getParameters(), poff = cam.getParameters();

        pon.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        poff.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);

        while(!requestStop)
        {
            try{
                cam.setParameters(pon);
                Thread.sleep(delay);
                cam.setParameters(poff);
                Thread.sleep(delayoff);
            }
            catch(InterruptedException ex)
            {

            }
            catch(RuntimeException ex)
            {
                requestStop = true;
                errorMessage = "Error setting camera flash status. Your device may be unsupported.";
            }
        }

        cam.release();

        isRunning = false;
        requestStop=false;

        controller.mHandler.post(controller.mShowToastRunnable);
    }

}
main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <SeekBar
        android:id="@+id/SeekBar01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:progress="0"
        android:layout_alignParentTop="true"
         >
    </SeekBar>

    <ImageButton
        android:id="@+id/btnSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/w_led_on"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"/>

</RelativeLayout>


以及它如何或为什么不起作用?我更新的说明说明了原因。这应该没有那么复杂,但Google或Stackoverflow上没有明确定义选通方法的答案。如果我们看不到你所做的,很难说出为什么它不起作用。@TimCastelijns谢谢,这就是我要找的!如果你想从github复制粘贴其他人的代码,至少有礼貌地给予他们信任至少我在StackOverFlow上提供了代码。。链接可能会被破坏。积极一点,这不是关于断开的链接或代码可用的地方,而是关于为别人的行为争光work@Bhaskar只有当主活动(手电筒)从清单应用程序活动中移除时,频闪活动才会启动。我怎样才能同时启动这两个呢?这段代码在所有类型的手机上都不起作用。我们如何使用surface view来实现这一点呢。