#

Android App: Flash Torch with Impulses

 

Diese Android App erstellt einen Toggle Button, welcher das Licht an dem Android Smartphone AN und AUS schaltet.

Zudem kann man auf Impuls Strobo schalten, woduch die HIGH Impuls und LOW Impulse als Blitze in der Handy Kamera angezeigt werden

 

mit Java Code in Android

 

 

Java Code

In java/project

MainActivity.java

 

 

package com.example.flashimpulser;

import androidx.appcompat.app.AppCompatActivity;

import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.ToggleButton;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

   
//--< Variables for Flash >--
   
boolean _isFlashAvailable;
    
CameraManager _CameraManager;
   
String _CameraId;
   
boolean _flashIsOn = false;
   
//--</ Variables for Flash >--

   
Switch _switchImpuler;
   
ToggleButton _toggleButton;
   
int _milliSeconds_HIGH =1000;
   
int _milliSedonds_LOW =1000;

   
//========< #region: Activity >========
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
//--------< onCreate() >--------
       
super.onCreate(savedInstanceState);
        setContentView(
R.layout.activity_main);

       
//----< init >----

       
_switchImpuler=findViewById(R.id.switchFlasher);
       
_toggleButton= findViewById(R.id.btnToggleLight);

       
//---< SeekBars >---
       
SeekBar seekbarHIGH =findViewById(R.id.seekBarHIGH);
        
_milliSeconds_HIGH =seekbarHIGH.getMax();
       
seekbarHIGH.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
           
@Override
           
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
               
_milliSeconds_HIGH =i;
            }

           
@Override
           
public void onStartTrackingTouch(SeekBar seekBar) {
            }

           
@Override
           
public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

       
SeekBar seekbarLOW =findViewById(R.id.seekBarLOW);
       
_milliSedonds_LOW =seekbarLOW.getMax();
       
seekbarLOW.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
           
@Override
           
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
               
_milliSedonds_LOW =i;
            }

           
@Override
           
public void onStartTrackingTouch(SeekBar seekBar) {
            }

           
@Override
           
public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });

       
//---< SeekBars >---

        //-< FlashLight >-
        //*flashlight is part of camera
       
_isFlashAvailable = getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
       
_CameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
       
try {
           
_CameraId = _CameraManager.getCameraIdList()[0];
        }
catch (CameraAccessException e) {
           
//e.printStackTrace();
       
}
       
//-</ FlashLight >-
        //----</ init >----
        //--------</ onCreate() >--------
   
}
   
//========</ #region: Activity >========

    //========< #region: Buttons+Views >========
   
public void btnToggleLight(View view) {
       
//--------< btnToggleLight() >--------
       
flashLightMain();
       
//--------</ btnToggleLight() >--------
   
}
   
public void switchImpulses(View view) {
        flashLightMain();
    }
   
//========</ #region: Buttons+Views >========


    //========< #region: Methods >========
   
private  void flashLightMain(){
       
//--------< flashLightMain() >--------


       
if (_toggleButton.isChecked() ==true) {
           
//----< Button==ON >----
           
if (_switchImpuler.isChecked())
            {
                
//--< Impulses >--
               
start_flash_Impule_HIGH();
               
//--</ Impulses >--
           
}
           
else
           
{
               
//--< Full >--
               
setFlashLightON();
               
//--</ Full >--
            
}
           
//----</ Button==ON >----
       
}
       
else
       
{
           
//----< Button==OFF >----
           
setFlashLightOFF();
            set_FlashImpulses_OFF();
           
//----</ Button==OFF >----
       
}
       
//--------</ flashLightMain() >--------
   
}

   
private void setFlashLightON()    {
       
//--------< setFlashLightON() >--------
        //*switch Flash Light ON / OFF
       
try {
               
_flashIsOn = true;
               
_CameraManager.setTorchMode(_CameraId, true);
        }
catch (CameraAccessException e) {
            e.printStackTrace();
        }
       
//--------</ setFlashLightON() >--------
   
}

   
private void setFlashLightOFF()    {
       
//--------< flashLightON() >--------
        //*switch Flash Light ON / OFF
       
try {
           
_flashIsOn = false;
           
_CameraManager.setTorchMode(_CameraId, false);
        }
catch (CameraAccessException e) {
            e.printStackTrace();
        }
       
//--------</ flashLightON() >--------
   
}



   
public void start_flash_Impule_HIGH()
    {
       
//----< start_flash_Impule_HIGH() >----
        //< check >
       
if (!_toggleButton.isChecked()) return;
       
if (!_switchImpuler.isChecked()) return;


        
Timer _timerPulsHIGH = new Timer();
       
_timerPulsHIGH.schedule(new TimerTask() {
           
@Override
           
public void run()
            {
               
try {
                   
_flashIsOn = true;
                   
_CameraManager.setTorchMode(_CameraId, true);
                    start_flash_Impule_LOW();
                }
               
catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        },
_milliSedonds_LOW);
       
//----</ Pstart_flash_Impule_HIGH() >--

   
}

   
public void start_flash_Impule_LOW()
    {
       
//----< start_flash_Impule_LOW() >----
       
Timer _timerPulsLOW = new Timer();
       
_timerPulsLOW.schedule(new TimerTask() {
           
@Override
            
public void run()
            {
               
try {
                   
_flashIsOn = false;
                   
_CameraManager.setTorchMode(_CameraId, false);
                    start_flash_Impule_HIGH();
                }
               
catch (CameraAccessException e) {
                    e.printStackTrace();
                }

            }
        },
_milliSeconds_HIGH);

       
//----</ start_flash_Impule_LOW() >--

   
}


   
private void set_FlashImpulses_OFF()
    {
       
//--------< set_FlashImpulses_OFF() >--------
        //--< Flashlight OFF >--
       
try {
           
_CameraManager.setTorchMode(_CameraId, false);
           
_flashIsOn = false;

        }
catch (CameraAccessException e) {
            e.printStackTrace();
        }
       
//--</ Flashlight OFF >--
        //--------</ set_FlashImpulses_OFF() >--------
   
}


   
//========</ #region: Methods >========
}

 

 

 

Layout:

Activity_main.xml

In res/layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".MainActivity"
   
android:orientation="vertical"
   
android:padding="44sp"
   
>


    <
ToggleButton
       
android:id="@+id/btnToggleLight"
       
android:layout_width="200sp"
       
android:layout_height="wrap_content"
       
android:layout_gravity="center_horizontal"
       
android:onClick="btnToggleLight"

       
/>

    <
Switch
       
android:id="@+id/switchFlasher"
       
android:layout_height="wrap_content"
       
android:layout_width="200sp"
       
android:layout_gravity="center_horizontal"
       
android:layout_margin="10sp"
       
android:text="@string/text_SwitchStrobo"
       
android:onClick="switchImpulses"

       
android:layout_marginBottom="44sp"
       
tools:ignore="UseSwitchCompatOrMaterialXml"
       
/>


    <
TextView
       
android:layout_width="match_parent"
        
android:layout_height="wrap_content"
       
android:text="@string/text_Slider_high"
       
android:textSize="16sp"
       
       
android:layout_marginTop="33dp"
       
/>

    <
SeekBar
       
android:id="@+id/seekBarHIGH"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_gravity="center_horizontal"
       
android:progress="1000"
       
android:max="1000"
       
/>

    <
TextView
       
android:layout_marginTop="11dp"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:text="@string/text_Slider_low"
       
android:textSize="16sp"
       
/>

    <
SeekBar
       
android:id="@+id/seekBarLOW"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:layout_gravity="center_horizontal"
       
android:progress="1000"
       
android:max="1000"
       
/>


</
LinearLayout>

 

 

 

AndroidManifest.xml

In der Android Manifest muss noch der Eintrag gemacht werden, dass FlashLight erlaubt ist

 

<uses-permission android:name="android.permission.FLASHLIGHT"/>

 

 

Mobile

.