0.6 flash binary code communication android source code flashlight mobile phone cipher frequency control as4.0 version password 123456 support all android2.2 to 10 and above systems Android 7.0 before and after 7.0 turn on the flash

Demo: 0.6 flashlight binary code communication android source code flashlight mobile phone cipher frequency control as4.0 version password 123456.apk

http://6.wjsou.com/uploads/1594994912599.apk

 

The projects developed before are running well. Today some people say that android10 does not light up. After finding the reason, it is compatible and supports all android2.2 to 10 and above systems. The flash is different before Android7.0 and after 7.0.

	package com.example.flashlampcontrol;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.Time;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 闪光灯的控制
 * 
 * @author 
 * 
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class MainActivity extends Activity {

    private Camera camera;
	private Parameters parameters;
    @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//判断API是否大于24(安卓7.0系统对应的API)
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

		}
		else
		{
			camera = Camera.open();
			camera.startPreview();
			parameters = camera.getParameters();
		}
    }

/**	打开或关闭手电筒
	 * 
	 */
	@TargetApi(Build.VERSION_CODES.N)
	public void changeFlashLight(boolean openOrClose) {
		if(openOrClose)
		{
			Log.i("tag", "~~~~~~~~~~~打开闪光灯~~~~~~~~~~~");
			//layout.setBackgroundColor(Color.RED);
		}
		else
		{
			Log.i("tag", "~~~~~~~~~~~关闭闪光灯~~~~~~~~~~~");
			//layout.setBackgroundColor(Color.BLACK);
		}

		//判断API是否大于24(安卓7.0系统对应的API)
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			try {
				//获取CameraManager
				CameraManager mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
				//获取当前手机所有摄像头设备ID
				String[] ids  = mCameraManager.getCameraIdList();
				for (String id : ids) {
					CameraCharacteristics c = mCameraManager.getCameraCharacteristics(id);
					//查询该摄像头组件是否包含闪光灯
					Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
					Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING);
					if (flashAvailable != null && flashAvailable
							&& lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
						//打开或关闭手电筒
						mCameraManager.setTorchMode(id, openOrClose);
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		else //以下是7.0以后闪光灯开启关闭代码
		{
			try {
				if(openOrClose)
				{
					parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);//开
					camera.setParameters(parameters);
				}
				else
				{
					parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); //关
					camera.setParameters(parameters);
				}

			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}






	// 关闭程序掉用处理部分
	public void onDestroy() {
		super.onDestroy();
		try {
			if(camera!=null)
			{
				camera.release(); //释放
				camera = null;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		android.os.Process.killProcess(android.os.Process.myPid()); // 终止线程
	}
}

 

Guess you like

Origin blog.csdn.net/chenhao0568/article/details/107419025