Android development ----------- improved flashlight version, android -----------
Based on the previous
Add a drawable/local_me.xml file under the res folder.
Localme_cmc
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/on" /> <!-- pressed --> <item android:drawable="@drawable/off" /> <!-- default/unchecked --></selector>
Main Program:
Package com. example. handlightactivity; import android. app. activity; import android. hardware. camera; import android. hardware. camera. parameters; import android. OS. bundle; import android. OS. process; import android. view. view; import android. view. view. onClickListener; import android. view. windowManager; import android. widget. toggleButton;/***** @ author love ** principle: * click the button to open the flashlight when you click the button. * Turn off the flashlight when you close the button. ***/public class MainActivity extends Activity implements OnClickListener {private ToggleButton toggleButton; private Camera camera Camera = Camera. open ();/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); toggleButton = (ToggleButton) this. findView ById (R. id. toggleButton1); toggleButton. setOnClickListener (this); // keep the screen getWindow (). addFlags (WindowManager. layoutParams. FLAG_KEEP_SCREEN_ON);} @ Override public void onClick (View v) {ToggleButton tb = (ToggleButton) v; Camera. parameters param = camera. getParameters (); if (! Tb. isChecked () {// open the camera flash! Param. setFlashMode (Camera. parameters. FLASH_MODE_TORCH); toggleButton. setBackgroundColor (0x30ffffff); // on open on.jpg tb. setBackgroundDrawable (getResources (). getDrawable (R. drawable. on);} else {param. setFlashMode (Camera. parameters. FLASH_MODE_OFF); toggleButton. setBackgroundColor (0 xffffffff); // off open the off.jpg tb. setBackgroundDrawable (getResources (). getDrawable (R. drawable. off);} camera. setParameters (param) ;}@ Override protected void onPause () {// camera. release (); // Process. killProcess (Process. myPid (); super. onPause ();}}
:
Android for flashlight Development
Set listening for open and close respectively, and move the last and second curly brackets before closeListener.
Public class MainActivity extends Activity {
Private Camera camera = null;
Private Parameters parameters = null;
Private Button open = null;
Private Button close = null;
Private TextView title = null;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
Open = (Button) findViewById (R. id. open );
Close = (Button) findViewById (R. id. close );
Title = (TextView) findViewById (R. id. title );
Title. setText (R. string. title );
Open. setText (R. string. open );
Close. setText (R. string. close );
Open. setOnClickListener (openListener );
Close. setOnClickListener (closeListener );
}
Public class openListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Camera = Camera. open ();
Parameters = camera. getParameters ();
Parameters. setFlashMode (Parameters. FLASH_MODE_TORCH); // enable
Camera. setParameters (parameters );
}
}
Public class closeListener implements OnClickListener {
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
Parameters. setFlashMode (Parameters. FLASH_MODE_OFF); // close
Camera. setParameters (parameters );
Camera. release ();
}
}
Public boolean onCreateOption... the remaining full text>
Where is the flashlight in Android 412?
You download a flashlight in the Android Market,
Generally, you use the flashlight that comes with your mobile phone.
Full Screen white lighting without flashing
Or if you download the program management software, some software comes with the flashlight function.