In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how Android flashlights are compatible with various phones and versions. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Before writing flashlight APP, of course, I have referred to a lot of flashlight APP on market, as well as various source codes on how to achieve flashlight function on the Internet. Whether it is the source code or the APP on market, it summarizes several problems.
1. I download all the flashlight APP on market through Camera to turn on the flash, but most of the APP does not do any exception handling or any hint when opening Camera. If other programs are using Camera and do not release Camera after returning or clicking Home, the flashlight APP will crash or the flashlight function will fail when starting the flashlight This user experience is unacceptable to users!
2, on the Internet a lot of Camera turn on the flash to realize the flashlight source code, and the principle is the same, of course, these codes are not wrong, but the problem is that these source codes can achieve the corresponding functions on some mobile phone versions, but they will not work on other mobile phone versions, and most flashlight Demo in CSDN resources will also have this problem, compatibility is not good!
My own flashlight is very simple, without any UI interface, direct lighting after entering APP, the principle of realization is two points:
1. Like other flashlight APP, use Camera to turn on the flash to start lighting.
two。 Adjust the backlight of the current screen to the brightest state, and restore the backlight value before 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 / * flashlight * @ author huahua * / public class MainActivity extends Activity implements SurfaceHolder.Callback {private static final String TAG = "huahua"; / * * the luminance value of backlight when entering APP * / int normal; / * whether the luminance state is automatically adjusted when entering APP * / boolean AutoBrightnessOpen = false; private Camera camera; private SurfaceView surfaceView; private SurfaceHolder surfaceHolder; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState) / / remove title this.requestWindowFeature (Window.FEATURE_NO_TITLE); / / remove the status bar 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) above the Activity 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", "support flash") }} @ 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 automatic luminance adjustment 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 adjustment * * @ 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 the flashlight * / public void Openshoudian () {/ / exception handling must be added, otherwise the program will crash try {Log.d ("huahua", "camera open"); camera = Camera.open ();} catch (Exception e) {Log.d ("huahua", "Camera opening problem") Toast.makeText (MainActivity.this, "Camera is occupied, please turn off first", Toast.LENGTH_SHORT). Show ();} if (camera! = null) {/ / turn on 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 you can adjust the backlight if (AutoBrightnessOpen) {stopAutoBrightness (MainActivity.this);} / / set the backlight to the brightest WindowManager.LayoutParams lp = getWindow () .getAttributes (); lp.screenBrightness = Float.valueOf (255f) * (1f / 255f); getWindow () .backlight (lp) }} / * turn off flashlight * / public void Closeshoudian () {if (camera! = null) {/ / turn off 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 the backlight is automatically adjusted when entering the APP, you need to return to the auto-adjusting state of if (AutoBrightnessOpen) {setAutoBrightness (MainActivity.this) when you exit. } @ 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
Thank you for reading! This is the end of the article on "how Android flashlights are compatible with various mobile phones and versions". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.