Home

Music

Video

Kanny

Android Studio Tutorial Flashlight (Simple Code)



MainActivity.Java

package com.example.duniyarhausaflashlighttutorial;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {

    private ImageView powerImageView;

    private CameraManager camManager;
    private Camera cam;

    private boolean lightIsOn = false;

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

        powerImageView = (ImageView) findViewById(R.id.iv_poweronoff);
        powerImageView.setOnClickListener(this);
    }

    private void flashLightOn() {
        // >= android 6.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (camManager == null) {
                camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
            }
            try {
                // Usually back camera is at 0 position.
                String cameraId = camManager.getCameraIdList()[0];
                camManager.setTorchMode(cameraId, true);
            } catch (Exception e) {
                showError(e);
            }
            return;
        }

        try {
            if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
                cam = Camera.open();
                Camera.Parameters p = cam.getParameters();
                p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                cam.setParameters(p);
                cam.startPreview();
            }
        } catch (Exception e) {
            showError(e);
        }
    }

    private void flashLightOff() {
        // >= android 6.0
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                String cameraId = camManager.getCameraIdList()[0];
                camManager.setTorchMode(cameraId, false);
            } catch (Exception e) {
                showError(e);
            }
            return;
        }

        try {
            if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
                cam.stopPreview();
                cam.release();
                cam = null;
            }
        } catch (Exception e) {
            showError(e);
        }
    }

    private void showError(Exception e) {
        String message = "Error: " + e.getMessage();
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();

        if (id == R.id.iv_poweronoff) {
            if (lightIsOn) {
                flashLightOff();
                powerImageView.setImageResource(R.drawable.poweroff);
            } else {
                flashLightOn();
                powerImageView.setImageResource(R.drawable.poweron);
            }
            lightIsOn = !lightIsOn;
        }
    }

}




activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <ImageView
        android:id="@+id/iv_poweronoff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/poweroff" />



</LinearLayout>





AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="12"
        android:targetSdkVersion="27" />

    <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:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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


</manifest>

Share this



Fb tt wp ig
Powered by DuniyarHausa and Developed By DuniyarHausa Web team