一個(gè)手電筒DEMO ,包含簡(jiǎn)單的app widget控制。
/**
* Created by? 小可愛兔寶貝 on 2022/2/24
* Mail:? zhaoshiyu900310@163.com
* Description:? ? 手電筒工具類? 初始化:FlashLightUtils.init();
*/
/**
* 開啟閃光燈
*/
? ? public static void openFlash() {
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (manager !=null) {
manager.setTorchMode("0", true);
? ? ? ? ? ? ? ? ? ? setFlashLightStatus(true);
? ? ? ? ? ? ? ? ? ? init();
? ? ? ? ? ? ? ? }
}else {
camera = android.hardware.Camera.open();
? ? ? ? ? ? ? ? android.hardware.Camera.Parameters parameters =camera.getParameters();
? ? ? ? ? ? ? ? parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
? ? ? ? ? ? ? ? camera.setParameters(parameters);
? ? ? ? ? ? ? ? camera.startPreview();
? ? ? ? ? ? ? ? setFlashLightStatus(true);
? ? ? ? ? ? ? ? init();
? ? ? ? ? ? }
}catch (Exception e) {
e.printStackTrace();
? ? ? ? }
}
/**
* 關(guān)閉閃光燈
*/
? ? public static void closeFlash() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
if (manager ==null) {
return;
? ? ? ? ? ? ? ? }
manager.setTorchMode("0", false);
? ? ? ? ? ? ? ? setFlashLightStatus(false);
? ? ? ? ? ? ? ? init();
//? ? ? ? ? ? ? ? manager=null;
? ? ? ? ? ? }catch (Exception e) {
e.printStackTrace();
? ? ? ? ? ? }
}else {
if (camera ==null) {
return;
? ? ? ? ? ? }
camera.stopPreview();
? ? ? ? ? ? camera.release();
? ? ? ? ? ? setFlashLightStatus(false);
? ? ? ? ? ? init();
//? ? ? ? ? ? camera=null;
? ? ? ? }
}
/**
* 獲取手電筒狀態(tài)
*
* @return 手電筒狀態(tài) true表示開啟? false表示關(guān)閉
*/
public static boolean getFlashLightStatus() {
return FlashLightStatus;
}
項(xiàng)目地址?https://github.com/zsyzsy1818/OneLight