开启android设备的闪光灯作为手电筒

手电筒是使用Android手机的闪光灯,所以需添加权限<uses-permission android:name="android.permission.CAMERA" />

我测试的手机是三星I9300,正常,界面没有美化就有一个按钮,代码如下:

package com.jwzhangjie;

import com.jwzhangjie.test.R;

import android.app.Activity;
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;

/**
 * 手电筒
 * @author jwzhangjie
 *
 */
public class CameraLight extends Activity{

	private Button openLight;
	private boolean status = false;
	private Camera camera;
	private Parameters parameters;
	private CameraLight instance;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cameralight);
		instance = this;
		camera = Camera.open();
		openLight = (Button)findViewById(R.id.openLight);
		openLight.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!status) {
					openLight.setText("关闭");
					status = true;
					new Thread(new TurnOnLight()).start();
				}else {
					status = false;
					openLight.setText("打开");
					instance.parameters.setFlashMode("off");
					instance.camera.setParameters(instance.parameters);
				}
			}
		});
		
	}
	
	
	private class TurnOnLight implements Runnable{
		@Override
		public void run() {
			instance.parameters = instance.camera.getParameters();
			instance.parameters.setFlashMode("torch");
			instance.camera.setParameters(instance.parameters);
		}
	}
 }



开启android设备的闪光灯作为手电筒,布布扣,bubuko.com

开启android设备的闪光灯作为手电筒

上一篇:Android源码研究的准备工作 -- 下载android源码到Ubuntu上(亲自调试,不能过分迷信GOOGLE文档)


下一篇:Android 4.4 Kitkat Phone工作流程浅析(一)__概要和学习计划