In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to achieve Android flashlight compatible with various mobile phones and versions, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
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); 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 luminance 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 auto-adjusted when entering APP, it needs to be restored to auto-adjustment state if (AutoBrightnessOpen) {setAutoBrightness (MainActivity.this) when exiting. } @ 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
The above is all the contents of the article "how to achieve flashlight compatibility with various mobile phones and versions in Android". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.