最近在学习Android的开发,写了个手电筒的案例,分享一下!//Activity.xml
package com.fq.flashlight;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class LightActivity extends Activity {
Button button = null;//创建Button类型的变量
TextView textView = null;//创建TextView类型的变量
Camera camera = null;//创建Camera类型的变量
Parameters params = null;//创建Parameters类型的变量
Boolean flag = true;//创建Boolean类型的变量,用于做是否开启的判断
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_light);
button = (Button)findViewById(R.id.button);//获取Button对象
textView = (TextView) findViewById(R.id.textView);//获取TextView对象
button.setOnClickListener(new OnClickListener() { //点击事件
@Override
public void onClick(View v) {
if(flag){
camera = Camera.open(); //获取摄像头
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
textView.setText(R.string.alert_on);
button.setText(R.string.button_off);
flag = false;
}else{
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
camera.release();
textView.setText(R.string.alert_off);
button.setText(R.string.button_on);
flag = true;
}
}
});
}
public void onBackPressed(){
//super.onBackPressed();
System.out.println("sdsfs");
new AlertDialog.Builder(this)
.setTitle("确定退出吗?")
.setIcon(R.drawable.ic_launcher)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
LightActivity.this.finish();
System.exit(0);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
})
.show();
}
}
BlueOcean手电筒
Hello world!
Settings
手电筒当前状态:开启
手电筒当前状态:关闭
开启手电
关闭手电
Author:BlueOcean
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="@string/alert_off"
android:textSize="22sp" />
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="@string/button_on" />
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView"
android:layout_centerVertical="true"
android:text="@string/author"
android:textAppearance="?android:attr/textAppearanceSmall" />
package="com.fq.flashlight"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="21" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name=".MainActivity"
android:label="@string/app_name" >
android:name=".LightActivity"
android:label="@string/app_name" >