-1

I am a developer just starting out with Android. I developed an app for Android that runs on tablet and phone which offers various services including flashlight.

I added this configuration in the manifest:

<Uses-feature android: name = "android.hardware.camera" android: required = "false" />
<Uses-feature android: name = "android.hardware.telephony" android: required = "false" />

I thought that with these two last optional features I could access the flash device when present on phone without exclude tablets to have the possibility to install the app but it doesnt work. Where am I wrong? Can someone please help me?

dario
  • 5,149
  • 12
  • 28
  • 32

1 Answers1

1

The above code just give the permission for the flashLight.

You have to write a code to turnon and off the Flash

I am adding the snippet of the demo i did for turning on and offing the flash light.

MainActivity.java

package com.example.balaji.myapplication;

import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;


public class MainActivity extends ActionBarActivity {


    private Camera cam;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button turnOn = (Button)findViewById(R.id.turnOnFlashLight);
        turnOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                turnOnFlashlight();
            }
        });

        Button turnOff = (Button)findViewById(R.id.turnOffFlashLight);
        turnOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                turnOffFlashlight();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void turnOnFlashlight(){
        cam = Camera.open();
        Camera.Parameters p = cam.getParameters();
        p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        cam.setParameters(p);
        cam.startPreview();
    }

    public void turnOffFlashlight(){
        cam.release();
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/turnOnFlashLight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Turn On"

        />

    <Button
        android:id="@+id/turnOffFlashLight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Turn Off"

        />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.balaji.myapplication" >


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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I am also giving the stackoverflow links for more information on this.

How to turn on FlashLight in Lollipop programmatically Android

Community
  • 1
  • 1
BalajiG
  • 544
  • 2
  • 15