This is a simple use of Android camera development flashlight function, Androidmanifest.xml file entrance is Startapp, this file is not uploaded, you can write.
Flashlight.java
Package Com.android.app;
Import android.app.Activity;
Import Android.hardware.Camera;
Import Android.hardware.Camera.Parameters;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.TextView;
Import Android.widget.Toast;
public class Main extends activity {Private Boolean isopent = false;
Private Camera Camera; @Override protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated Method stub super.oncreate (sav
Edinstancestate);
View view = View.inflate (this, r.layout.main, null);
Setcontentview (view);
TextView img_but = (TextView) Findviewbyid (r.id.main_img); Img_but.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {//TODO Auto-ge
nerated method Stub if (!isopent) {Toast.maketext (Getapplicationcontext (), "You have turned on the flashlight", 0). Show ();
Camera = Camera.open ();
Parameters params = Camera.getparameters ();
Params.setflashmode (Parameters.flash_mode_torch); Camera.setparameters (params); Camera.startpreview ();
Start light isopent = true;
else {Toast.maketext (Getapplicationcontext (), "Turn off Flashlight", Toast.length_short). Show (); Camera.stoppreview (); Turn off the light camera.release ();
Turn off the camera isopent = false;
}
}
});
}
}
Layout file Code
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"<a href=" http:// Schemas.android.com/apk/res/android "" "Rel=" nofollow "target=" _blank ">http://schemas.android.com/apk/res/ Android "</a>
android:layout_width=" match_parent "
android:layout_height=" Match_parent
" android:orientation= "vertical" >
<textview
android:id= "@+id/main_img"
Fill_parent "
android:layout_height=" fill_parent "
android:background=" @drawable/main_body ">
</TextView>
</LinearLayout>
Androidmanifest.xml file
<manifest xmlns:android= "<a href=" http://schemas.android.com/apk/res/android "rel=" nofollow "target=" _blank
">http://schemas.android.com/apk/res/android" </a> package= "Com.android.app" android:versioncode= "1" Android:versionname= "1.0" > <uses-sdk android:minsdkversion= "8" android:targetsdkversion= ""/> <appl
Ication android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= ". AppStart "> <intent-filter> <action android:name=" Android.intent.action.MAIN "/> <categor Y android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity Andro Id:name= ". Main "> </activity> </application> <!--camera, flashlight--> <uses-permission android:name=" android.pe Rmission. CAMERA "/> <uses-permission android:name=" Android.permission.FLASHLIGHT "/> <uses-feature android:naMe= "Android.hardware.camera"/> <uses-feature android:name= "Android.hardware.camera.autofocus"/> <
Uses-feature android:name= "Android.hardware.camera.flash"/> </manifest>
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.