Android Universal Flashlight Code _android

Source: Internet
Author: User
Tags static class

A few days ago this side of a community power, black Ma Ma, a cat broke into the dormitory, frighten me into tears, and then decisively wrote a flashlight, the next power outage, again, I want to frighten it.

I saw a lot of people are directly call the flash, in fact, most of the models are not supported, I this is to use the camera function to achieve, most of the models are supported.

Mainactivity.class

public class Mainactivity extends fragmentactivity {@Override protected void onCreate (Bundle savedinstancestate) {
    Super.oncreate (savedinstancestate);
 
    Setcontentview (R.layout.activity_main); if (isflashsupported ()) {if (savedinstancestate = = null) {Getsupportfragmentmanager (). BeginTransaction (). A
      DD (R.id.container, New Flashlightfragment ()). commit ();
    } else {Shownoflashalert (); }} private void Shownoflashalert () {new Alertdialog.builder (this). Setmessage (R.string.device_not_supp ort_msg). SetIcon (Android. R.drawable.ic_dialog_alert). Settitle (R.string.device_not_support_title). Setpositivebutton (R.string.ok, New Dialo
            Ginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {
            Dialog.dismiss ();
          Finish ();
  ). Create (). Show (); Private Boolean isflashsupported () {Packagemanager pm = GEtpackagemanager ();
  Return Pm.hassystemfeature (Packagemanager.feature_camera_flash); public static class Flashlightfragment extends Fragment implements Surfaceholder.callback {public FLASHLIGHTF
    Ragment () {} ImageButton Btnswitch;
    ImageView BG;
 
    Private Boolean Islighon = false;
    Private Camera Camera;
 
    Camera.parameters params;
      @Override public void OnStart () {Super.onstart ();
      Surfaceview preview = (surfaceview) GetView (). Findviewbyid (R.id.preview);
      Surfaceholder Mholder = Preview.getholder ();
    Mholder.addcallback (this);
      @Override public void OnPause () {super.onpause ();
    Turnoffflash ();
      @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
 
      View view = Inflater.inflate (R.layout.fragment_main_activity, container, false); Flash switch button Btnswitch = (ImageButton) View.findviewbyid (R.id.flasHlight_button);
 
      BG = (ImageView) View.findviewbyid (R.ID.FLASHLIGHT_BG);
 
      displaying button image togglebgimage ();
 
        Switch button click event to toggle Flash On/off btnswitch.setonclicklistener (new View.onclicklistener () {
          @Override public void OnClick (View v) {if (Islighon) {Turnoffflash ();
          else {turnonflash ();
 
      }
        }
      });
    return view;  }//turning on flash private void Turnonflash () {if (!islighon) {if (camera = = NULL | | params
        = = null) {return;
        } params = Camera.getparameters ();
        Params.setflashmode (Camera.Parameters.FLASH_MODE_TORCH);
        Camera.setparameters (params);
        Camera.startpreview ();
 
        Islighon = true;
      Togglebgimage (); }//turning off flash private void Turnoffflash () {if (Islighon) {if (camera = = NULL || ParAMS = null) {return;
        } params = Camera.getparameters ();
        Params.setflashmode (Camera.Parameters.FLASH_MODE_OFF);
        Camera.setparameters (params);
        Camera.stoppreview ();
 
        Islighon = false;
      Togglebgimage ();
      }} private void Togglebgimage () {if (Islighon) {bg.setimageresource (r.mipmap.f_on);
      else {bg.setimageresource (r.mipmap.f_off);
 
    @Override public void surfacechanged (surfaceholder holder, int format, int width, int height) {} @Override public void surfacedestroyed (Surfaceholder holder) {if (camera!= null) {CAMERA.STOPPR
        Eview ();
        Camera.setpreviewcallback (NULL);
        Camera.release ();
      camera = null; @Override public void surfacecreated (Surfaceholder holder) {if (camera = = null) {camera
        = Camera.open ();
        params = Camera.getparameters ();
    try {      Camera.setpreviewdisplay (holder);
          catch (IOException e) {camera.release ();
        camera = null;
    } turnonflash ();

 }
 
  }
 
 
}

Fragment_main_activity.xml

<relativelayout
  xmlns:android= "http://schemas.android.com/apk/res/android"
  android:layout_width= " Match_parent "
  android:layout_height=" match_parent "
  >
 
  <surfaceview
    android:id=" @+id/ PREVIEW "
    android:layout_width=" 1DP "
    android:layout_height=" 1DP "/>
 
  <imageview
    android: Id= "@+id/flashlight_bg"
    android:layout_width= "match_parent"
    android:layout_height= "Match_parent"
    android:src= "@mipmap/f_on"
    android:scaletype= "Centercrop"
    />
 
  <imagebutton
    Android:id= "@+id/flashlight_button"
    android:layout_width= "100DP"
    android:layout_height= "200DP"
    Android:layout_centerinparent= "true"
    android:background= "@android: color/transparent"
    android:text= " Fuck "/>
 
</RelativeLayout>

Activity_main.xml

<framelayout xmlns:android= "http://schemas.android.com/apk/res/android"
       android:id= "@+id/container"
       Android:layout_width= "Match_parent"
       android:layout_height= "match_parent"
       />

Androidmanifest.xml

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" Android "package=" Com.liujinghuiuncleapp.app.tool.flashlight "> <uses-permission android:name=" Android.permission.CAMERA "/> <uses-permission android:name=" Android.permission.FLASHLIGHT "/> <uses-fe Ature android:name= "Android.hardware.camera"/> <uses-feature android:name= "Android.hardware.camera.flash"/ > <application android:allowbackup= "true" android:icon= "@mipmap/ic_launcher" android:theme= "@style/A Pptheme "android:label=" @string/app_name "> <activity android:name=". Mainactivity "android:label=" @string/app_name "> <intent-filter> <action android:name=" and Roid.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </int
 ent-filter> </activity> </application> </manifest>

The above is the entire contents of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.