android闪关灯的开启和关闭方法代码实例
1、activity_main.xml 两个按钮,一个open一个close
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingbottom="@dimen/activity_vertical_margin"
android:paddingleft="@dimen/activity_horizontal_margin"
android:paddingright="@dimen/activity_horizontal_margin"
android:paddingtop="@dimen/activity_vertical_margin"
tools:context=".mainactivity" >
<textview
android:id="@+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<button
android:id="@+id/button_open"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignleft="@+id/textview1"
android:layout_below="@+id/textview1"
android:layout_margintop="38dp"
android:text="open" />
<button
android:id="@+id/button_close"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignleft="@+id/button_open"
android:layout_below="@+id/button_open"
android:layout_margintop="50dp"
android:text="close" />
</relativelayout>
2、mainactivity 核心代码
package com.qiantu.flashlight;
import android.app.activity;
import android.hardware.camera;
import android.hardware.camera.parameters;
import android.os.bundle;
import android.view.menu;
import android.view.view;
import android.view.view.onclicklistener;
import android.widget.button;
public class mainactivity extends activity {
private camera camera;
private button openbtn;
private button closebtn;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
openbtn = (button) findviewbyid(r.id.button_open);
openbtn.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
if (null != camera) {
parameters parameters = camera.getparameters();
parameters.setflashmode(parameters.flash_mode_torch);
camera.setparameters(parameters);
}
}
});
closebtn = (button) findviewbyid(r.id.button_close);
closebtn.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
if (null != camera) {
parameters parameters = camera.getparameters();
parameters.setflashmode(parameters.flash_mode_off);
camera.setparameters(parameters);
}
}
});
}
@override
protected void onstart() {
super.onstart();
camera = camera.open();
}
@override
protected void ondestroy() {
super.ondestroy();
if (null != camera) {
camera.release();
}
}
@override
public boolean oncreateoptionsmenu(menu menu) {
getmenuinflater().inflate(r.menu.main, menu);
return true;
}
}
3、androidmanifest.xml 加入摄像头和闪关灯的权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.qiantu.flashlight"
android:versioncode="1"
android:versionname="1.0" >
<uses-sdk
android:minsdkversion="8"
android:targetsdkversion="18" />
<!-- 添加的部分 -->
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.autofocus" />
<uses-permission android:name="android.permission.camera" />
<uses-permission android:name="android.permission.flashlight" />
<uses-permission android:name="android.permission.disable_keyguard" />
<!-- end -->
<application
android:allowbackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/apptheme" >
<activity
android:name="com.qiantu.flashlight.mainactivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.main" />
<category android:name="android.intent.category.launcher" />
</intent-filter>
</activity>
</application>
</manifest>
上一篇: 发现身边的美 地铁主题摄影拍摄小窍门
下一篇: 揭秘:宋代的服饰风格是怎样演变的?