Smartphone lantern flashlight on/off

Hello, dear friends, I am getting started in the programming for android, and I want to do something simple, by pressing the screen to turn on / off the flashlight of the smartphone.

Does anyone know how to do it?

Cheers

int uX, uY;
color bgColor;
  void setup(){
  size(400,500);
  uX=width/14;
  uY=height/14;

}

  void draw(){
  background(bgColor);
  if(mousePressed)bgColor=255;//ON
  else bgColor=127;//OFF
  }

I tell you that I am improving the “GUI” for the flashlight, I want to contribute with the code of this “toogle button” in case someone helps you

verticalToggle VT;
int uX, uY;
color bgColor;
  void setup(){
  size(400,500);
  uX=width/14;
  uY=height/14;
 //verticalToggle(Xpos, Ypos, width, height, backColor, buttonColor, textColor, TextWhenOFF, textWhenOn);
  VT = new verticalToggle(4*uX,uY,6*uX,12*uY,color(127,127,0),color(255,255,0),color(0),"OFF","ON");
}

void draw(){
  background(bgColor);
  VT.drawToggle();
  if(mousePressed || VT.value)bgColor=255;//ON
  else bgColor=127;//OFF
  }
  
class verticalToggle{
  String lbl1, lbl2;
  int  x, y, w, h;
  float slvalue;
  boolean value;
  color col, txtCol, color1, color2, textColor;
   verticalToggle(int  _x, int  _y, int  _w, int  _h, color _color1, color _color2, color _textColor, String _lbl1, String _lbl2){
    x=_x;
    y=_y;
    w=_w;
    h=_h;
    color1=_color1;
    color2=_color2;
    textColor=_textColor;
    textSize(h/8);
    textAlign(CENTER);
    lbl1=_lbl1;
    lbl2=_lbl2;
  }
  
    
  void drawToggle() {
  fill(color1);  
  rect(x,y,w,h,30);
      if(value){
      pushMatrix();
      translate(x,y);
      fill(color2);
      rect(0,0,w,h/2,30);
      fill(textColor);
      textSize(h/8);
      textAlign(CENTER);      
      text(lbl2, (w)/2, (1.3*h)/2);
      popMatrix();
    }else{
      pushMatrix();
      translate(x,y);
      fill(color2);
      rect(0,h/2,w,h/2,30);
      fill(textColor);
      textSize(h/8);
      textAlign(CENTER);
      text(lbl1, (w)/2, (1.3*h)/2);
      popMatrix();
    }
  if (mouseX>x && mouseX<x+w && mouseY>y && mouseY<y+h/2) {
    if (mousePressed) {
      value=true;
  }
  }else if (mouseX>x && mouseX<x+w && mouseY>y+h/2 && mouseY<y+h) {
        if (mousePressed) {
      value=false;
  }
  }
}
}

Hi @dinoelectro Welcome to the forum.
I think you can find something useful here,

1 Like

@dinoelectro , @noel ===

The code you have linked has some problems:

  • As it is it will not work while targetting more than Lollypop; after you have to ask for permissions on runtime and not in the Manifest.
  • In the Manifest you have to add an uses feature : uses-feature android:name=“android.hardware.camera” - This is mandatory for a play release
  • Before trying tolaunch the camera and put the flash light on you have to verify that the flash light is available on the phone: in a lot of cases (tablets like nexus7 or 10) that is not true in order to do that you create a boolean isFlashAvailable, setting it to false, then:
Activity act = this.getActivity();
isFlashAvailable = act.getApplicationContext().getPackageManager()
        .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        
        if(!isFlashAvailable){
       String  leTxt = "Cet appareil n'a pas de flash!"; 
///here do what you want: finish or return or...
        }
  • You have also to turn off the flash using the onPause() and not only with onStop();
  • And finally you have to set the orientation: see what happens if you dont and turn the phone…
2 Likes

Thanks @akenaton for revising.
I will correct that.
If you have some time it would be great if you could revise the others as well. (once in a while :smile:)

@noel ===
ok; this repo is a good idea (i never saw it before, excuse me!)…
As for the torch i found another problem with this code (perhaps related to API???): the getParameters() method from the camera is not called at the good moment and it returns empty parameters; you have to call it overriding onStart(); if it is usefull i can put the whole code, tested with Huawei nougat; but as it is for homework i dont like to do that…

2 Likes

Yes I saw that it is homework, and he already did his part, but I think that you can’t expect one can write the demanded android code programmatically right in the beginning. And that is why I did the repro. I don’t see the harm in that. But I’m glad you liked the idea.

1 Like

thanks, I’m experimenting with the code you shared … I don’t have a final result, once I get it I share it.

Also I will try to turn on / off the flashlight using voice commands. Do you have any suggestion?

regards!

You don’t need to write code for that. Just say “Ok Google. Turn on flashlight” e voila! :slight_smile:
But you can open an app and write a code to execute whatever you want. But you have to prepare your app, because Ok Google will not open any third party app. Maybe this is a place to start.

1 Like

@noel ===
I must say that i am always very disturbed with “homework”; you wrote that it is impossible to ask a beginner to write android code, but in this case why the teacher has choosen to ask for???
As for voice commands i think that the forum has to avoid “cascade” problems; when the first and main one is solved, open a new one!

1 Like

@akenaton
I didn’t say that it’s impossible. It’s just hard because P4A has no documentation
for working with views etc. You yourself have pointed out many times to use Android Studio instead for such tasks, and use Processing as an excellent graphics lib. I don’t think that a teacher will use A4P for actually teaching Android.
Another reason why I would like to facilitate P4A with some ready code is that instead of using P4A with the Arduino Ide (which were born to live together); P4A has totally lost the battle against software like “MIT App Inventor” because it seems more simple. And that’s really a pity.
I think many people come here to build an app for IoT purposes and they can’t even find a decent Bluetooth communication code. So you have to find a balance between giving a code to start with or trying to force someone to learn things bit by bit and taking the risk of just scare them away.

1 Like