-1

I want to create an app for Android devices that will have 2 buttons, one button for "ON" and the other for "OFF". I have searched the net for an example and I have seen many examples, but they were very bad organized (for me). what I have created so far is the layout with the buttons and the code of the main activity is:

 package com.simple.flashlight;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Camera;
import android.hardware.Camera.Parameters;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class Main extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_menue);

        Button turnOnButtOn = (Button) findViewById (R.id.button1);
        Button turnOnButtOff = (Button) findViewById (R.id.button2);


        turnOnButtOn.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View cameraButton){

            }
        });
    }
}

now I want to do something like this (pseud code):

turnOnButtOn.setOnClickListener(new Button.OnClickListener(){
    public void onClick(View cameraButton){
        turnOnTheFlahLight
    }
});

turnOnButtOn.setOnClickListener(new Button.OnClickListener(){
    public void onClick(View cameraButton){
        turnOffTheFlahLight
    }
});

If there any full reference (I am new to android) it would be nice. Thanks

il_guru
  • 8,383
  • 2
  • 42
  • 51
vlio20
  • 8,955
  • 18
  • 95
  • 180
  • As far as control over the flashlight, you should look at http://stackoverflow.com/questions/6068803/how-turn-on-only-camera-flash-light-programmatically-in-android – Femi Nov 01 '12 at 22:22
  • it seems like a great answer but for begginer like me it would be nice to have all of the code, including the import and the integration of the buttons in this code – vlio20 Nov 01 '12 at 22:26

3 Answers3

3

Firstly you need to add a permission to your manifest to use the flashlight:

 <permission android:name="android.permission.FLASHLIGHT"
             android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
             android:protectionLevel="normal" />

To turn the flashlight on use something like this:

Camera mCam = Camera.open();     
Parameters p = mCam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
mCam.setParameters(p);
mCam.startPreview();

To turn it off:

mCam.stopPreview();
  mCam.release();
Pete
  • 597
  • 1
  • 8
  • 27
2

If you want to turn on the LED of a device to make it act like a flashlight, which is what your pseudocode is indicating, your app has to pretend like it's a camera app because the flash is within the camera API. Therefore you have to do a few things.
1) In your manifest you need to declare that your application uses the camera permission and feature
2) In your application code, you need to actually create an instance of a Camera object using Camera::open()
3) Your application code will need to create a CameraParams object and set the flash mode to on or torch (it's been a couple months since I looked at this but one of the two will do it). Check the CameraParemeters docs for more details. Once you set the proper parameters in this object then you need to call setParameters on the Camera object you created in step 2
4) Then you need to call startPreview()

To turn the flash off you should just need to call stopPreview() on your Camera object

David C. Sainte-Claire
  • 2,461
  • 1
  • 15
  • 12
0

flash light app isnt so nice for beginners if you have the wrong device, because some manufactures dont use the normal way android provides for putting on the light. I recommend you to do some other stuff to learn android.

Fabian Knapp
  • 1,342
  • 13
  • 27