Before writing the flashlight APP, of course, I have referenced many flashlight APPs on the market, as well as various source codes for how to implement the flashlight function on the Internet. Whether it is the source code or the APP on the market, I have summarized several issues.
1. All flashlight APPs on the market I downloaded are implemented by Camera turning on the flash, but most APPs do not perform exception handling when opening Camera, and there is no prompt. If other programs use Camera and return or click Home, the corresponding flashlight APP will crash or the flashlight function has expired. This kind of user experience is unacceptable to users!
2. Many Cameras on the Internet turn on the flashlight to realize the source code of the flashlight, and the principles are the same. Of course, these codes are not errored, but the problem is that these source codes can implement corresponding functions on some mobile phone versions, but they will not work on other mobile versions. Most flashlight demos in CSDN resources will also have this problem, and the compatibility is not good!
The flashlight I implemented myself is very simple, without any UI interface, it will be illuminated directly after entering the APP, and the implementation principle is 2 points:
1. Like other flashlight APPs, turn on the flash with Camera to start lighting
2. Adjust the backlight of the current screen to the brightest state, and restore the previous backlight value when exiting.
The code and layout are as follows:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Huahua flashlight * @author huahua */ public class MainActivity extends Activity implements { private static final String TAG = "huahua"; /** * Backlight brightness value when entering the APP */ int normal; /** * When entering the APP, is it automatically adjusted brightness status? */ boolean AutoBrightnessOpen = false; private Camera camera; private SurfaceView surfaceView; private SurfaceHolder surfaceHolder; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); //Remove title (Window.FEATURE_NO_TITLE); //Remove the status bar on the Activity getWindow().setFlags(.FLAG_FULLSCREEN, .FLAG_FULLSCREEN); setContentView(.activity_main); surfaceView = (SurfaceView) (); surfaceHolder = (); (this); (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); if(isAutoBrightness(getContentResolver())) { AutoBrightnessOpen = true; } normal = (getContentResolver(), .SCREEN_BRIGHTNESS, 255); PackageManager pm= (); FeatureInfo[] features=(); for(FeatureInfo f : features) { if(PackageManager.FEATURE_CAMERA_FLASH.equals()) //Discern whether the device supports flash { ("huahua","Support flash"); } } } @Override protected void onPause() { // TODO Auto-generated method stub (); Closeshoudian(); } @Override protected void onResume() { // TODO Auto-generated method stub (); Openshoudian(); } /** * Determine whether automatic brightness adjustment is turned on * * @param aContext * @return */ public boolean isAutoBrightness(ContentResolver aContentResolver) { boolean automicBrightness = false; try { automicBrightness = (aContentResolver, .SCREEN_BRIGHTNESS_MODE) == .SCREEN_BRIGHTNESS_MODE_AUTOMATIC; } catch (SettingNotFoundException e) { (); } return automicBrightness; } /** * Stop automatic brightness adjustment * * @param activity */ public void stopAutoBrightness(Activity activity) { ((), .SCREEN_BRIGHTNESS_MODE, .SCREEN_BRIGHTNESS_MODE_MANUAL); } /** * Restore automatic brightness adjustment * * @param activity */ public void setAutoBrightness(Activity activity) { ((), .SCREEN_BRIGHTNESS_MODE, .SCREEN_BRIGHTNESS_MODE_AUTOMATIC); } /** * Turn on the flashlight */ public void Openshoudian() { //Exception handling must be added, otherwise the program will crash if Camera fails to open. try { ("huahua","camera open"); camera = (); } catch (Exception e) { ("huahua","There is a problem with Camera opening"); (, "Camera is occupied, please close first", Toast.LENGTH_SHORT).show(); } if(camera != null) { //Open the flash (); parameter = (); (.FLASH_MODE_TORCH); (parameter); ("huahua","Flash on"); //Open the automatic backlight adjustment function first before adjusting the backlight if(AutoBrightnessOpen) { stopAutoBrightness(); } //Set the backlight to brightest lp = getWindow().getAttributes(); = (255) * (1f / 255f); getWindow().setAttributes(lp); } } /** * Turn off the flashlight */ public void Closeshoudian() { if (camera != null) { // Turn off the flash ("huahua", "closeCamera()"); ().setFlashMode(.FLASH_MODE_OFF); (()); (); (); camera = null; //Restore the backlight value before entering the program lp = getWindow().getAttributes(); = (normal) * (1f / 255f); getWindow().setAttributes(lp); //If the backlight is automatically adjusted when entering the APP, it needs to be restored to the automatic adjustment state when exiting. if(AutoBrightnessOpen) { setAutoBrightness(); } } } @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) { (holder); } } catch (IOException e) { (); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } }
activity_main.xmlactivity_main.xml
<LinearLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <SurfaceView android: android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF"/> </LinearLayout>
Okay, friends who are interested can download the source code, and detailed comments have been added to the code.
If you find that the debugging is not running properly after downloading, you can feedback and communicate together
Source codeDownload address
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.