Android Flashlight is compatible with all mobile phones and versions

  • 2021-11-13 02:43:31
  • OfStack

Before writing flashlight APP, of course, I have referred to many flashlights APP on market, and how to realize various source codes of flashlight functions on the Internet. Whether it is the source code or APP on market, several problems are summarized.

1. I downloaded all the flashlights on market. APP was realized by turning on the flash on Camera. However, most APP does not handle exceptions when opening Camera, and there is no prompt. If other programs use Camera and return or point Home without releasing Camera, then the corresponding flashlight APP will crash or the flashlight function has failed. This user experience is unacceptable to users!

2, a lot of Camera on the Internet to open the flashlight flashlight source, And the principle is 1, of course, there is no error in these codes, but the problem is that these source codes can achieve corresponding functions on some mobile phone versions, but they don't work on other mobile phone versions. Most flashlights Demo in CSDN resources will also have this problem, and the compatibility is not good!

The flashlight I realized by myself is very simple. There is no UI interface. After entering APP, it will be illuminated directly. The principle of realization is 2 points:

1. Like other flashlights APP1, turn on the flash with Camera to start lighting

2. Adjust the backlight of the current screen to the brightest state, and the backlight value before restoration when exiting

The code and layout are as follows:

MainActivity.java


package huahua.flashlight;
 
import java.io.IOException;
 
import android.app.Activity;
import android.content.ContentResolver;
import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
 
/**
 *  Flower flashlight 
 * @author huahua
 */
public class MainActivity extends Activity implements SurfaceHolder.Callback{
 private static final String TAG = "huahua"; 
 /**
 *  Enter APP Backlight brightness value when 
 */
 int normal;
 /**
 *  Enter APP Is it in the automatic brightness adjustment state 
 */
 boolean AutoBrightnessOpen = false;
 
 private Camera camera; 
 
 private SurfaceView surfaceView; 
 private SurfaceHolder surfaceHolder; 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 
 // Removal title 
 this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
  
 // Remove Activity The status bar above  
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
 WindowManager.LayoutParams.FLAG_FULLSCREEN); 
 
 setContentView(R.layout.activity_main);
 
 surfaceView = (SurfaceView) this.findViewById(R.id.surfaceview); 
 surfaceHolder = surfaceView.getHolder(); 
 surfaceHolder.addCallback(this); 
 surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
 
 if(isAutoBrightness(getContentResolver()))
 {
 AutoBrightnessOpen = true;
 }
 
 normal = Settings.System.getInt(getContentResolver(), 
  Settings.System.SCREEN_BRIGHTNESS, 255); 
 
 PackageManager pm= this.getPackageManager();
 FeatureInfo[] features=pm.getSystemAvailableFeatures();
 for(FeatureInfo f : features)
 {
 if(PackageManager.FEATURE_CAMERA_FLASH.equals(f.name)) // Determine whether the device supports flash 
 {
 Log.d("huahua"," Flash supported ");
 }
 }
 
 }
 @Override
 protected void onPause() {
 // TODO Auto-generated method stub
 super.onPause();
 
 Closeshoudian();
 }
 
 @Override
 protected void onResume() {
 // TODO Auto-generated method stub
 super.onResume();
 
 Openshoudian();
 }
 
 /**
 *  Determine whether the automatic brightness level is turned on 
 * 
 * @param aContext
 * @return
 */
 public boolean isAutoBrightness(ContentResolver aContentResolver) {
 boolean automicBrightness = false;
 try {
  automicBrightness = Settings.System.getInt(aContentResolver,
   Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
 } catch (SettingNotFoundException e) {
  e.printStackTrace();
 }
 return automicBrightness;
 }
 
 /**
 *  Stop automatic brightness adjustment 
 * 
 * @param activity
 */
 public void stopAutoBrightness(Activity activity) {
 Settings.System.putInt(activity.getContentResolver(),
  Settings.System.SCREEN_BRIGHTNESS_MODE,
  Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
 }
 
 /**
 *  Restore automatic brightness level 
 * 
 * @param activity
 */
 public void setAutoBrightness(Activity activity) {
 Settings.System.putInt(activity.getContentResolver(),
  Settings.System.SCREEN_BRIGHTNESS_MODE,
  Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
 }
 
 /**
 *  Turn on a flashlight 
 */
 public void Openshoudian()
 {
 // Exception handling 1 Be sure to add, otherwise Camera If it fails to open, the program will crash 
 try {
  Log.d("huahua","camera Open ");
 camera = Camera.open(); 
 } catch (Exception e) {
 Log.d("huahua","Camera There is a problem opening ");
 Toast.makeText(MainActivity.this, "Camera Occupied, please close first ", Toast.LENGTH_SHORT).show();
 }
 
 if(camera != null)
 {
 // Turn on the flash 
 camera.startPreview(); 
 Camera.Parameters parameter = camera.getParameters(); 
 parameter.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); 
 camera.setParameters(parameter);
 Log.d("huahua"," Flash on ");
 
 // Turn off the automatic backlight adjustment function before adjusting the backlight 
 if(AutoBrightnessOpen)
 {
 stopAutoBrightness(MainActivity.this);
 }
 
 // Set the backlight to the brightest 
  WindowManager.LayoutParams lp = getWindow().getAttributes();
  lp.screenBrightness = Float.valueOf(255) * (1f / 255f);
  getWindow().setAttributes(lp);
 }
 }
 
 /**
 *  Turn off the flashlight 
 */
 public void Closeshoudian()
 {
 if (camera != null)
 {
  // Turn off the flash 
  Log.d("huahua", "closeCamera()");
 camera.getParameters().setFlashMode(Camera.Parameters.FLASH_MODE_OFF); 
 camera.setParameters(camera.getParameters());
  camera.stopPreview();
  camera.release();
  camera = null;
  
  // Restore the backlight value before entering the program 
  WindowManager.LayoutParams lp = getWindow().getAttributes();
  lp.screenBrightness = Float.valueOf(normal) * (1f / 255f);
  getWindow().setAttributes(lp);
  
  // If you enter APP When the backlight is automatically adjusted, it needs to be restored to the automatic adjustment state when exiting 
 if(AutoBrightnessOpen)
 {
 setAutoBrightness(MainActivity.this);
 }
 }
 }
 
 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width,
 int height) {
 // TODO Auto-generated method stub
 
 }
 @Override
 public void surfaceCreated(SurfaceHolder holder) {
 try { 
 if(camera != null)
 {
 camera.setPreviewDisplay(holder); 
 }
 } catch (IOException e) { 
 e.printStackTrace(); 
 } 
 
 }
 
 @Override
 public void surfaceDestroyed(SurfaceHolder holder) {
 // TODO Auto-generated method stub
 
 }
 
 
}

activity_main.xmlactivity_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:orientation="vertical"
 tools:context=".MainActivity" >
 
 <SurfaceView
 android:id="@+id/surfaceview"
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:background="#FFFFFF"/>
 
</LinearLayout>

Ok, interested friends can download the source code, and detailed comments have been added to the code

If debugging after downloading finds that it is not running normally, you can feedback 1 communication

Source code download address


Related articles: