0.6闪光灯二进制编码通信android源码手电筒手机密码器频率控制 as4.0版 密码123456 支持所有android2.2到10及以上系统 Android7.0以前和7.0以后开启闪光灯

演示:0.6闪光灯二进制编码通信android源码手电筒手机密码器频率控制 as4.0版 密码123456.apk

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

 

之前开发的项目,运行好好的。今天有人说android10不亮,查到原因后做了下兼容,支持所有android2.2到10及以上系统。Android7.0以前和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()); // 终止线程
	}
}

 

你可能感兴趣的:(Android复习)