码迷,mamicode.com
首页 > 移动开发 > 详细

Android--sos闪光灯

时间:2016-10-14 19:59:47      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:

Camera camera = null;
Parameters parameters = null;

Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
}

};

// 主开启
public void start() {
handler.post(startThread);
handler.post(closeThread);
}

// 关闭
public void close() {
handler.removeCallbacks(startThread);
handler.removeCallbacks(closeThread);
flashclose();
camera.stopPreview();
camera.release();
camera = null;
}

private void flashopen() {//开启灯
if (camera == null) {
camera = Camera.open();
}
parameters = camera.getParameters();

parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);

camera.setParameters(parameters);
camera.startPreview();
}

private void flashclose() {//关闭灯
if (camera == null) {
camera = Camera.open();
}
parameters = camera.getParameters();

parameters.setFlashMode(Parameters.FLASH_MODE_OFF);

camera.setParameters(parameters);
}

Runnable startThread = new Runnable() {
// 将要执行的操作写在线程对象的run方法当中
public void run() {
System.out.println("updateThread");
flashopen();
try {
Thread.sleep(100);
flashclose();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.post(startThread);
}

};

Runnable closeThread = new Runnable() {
// 将要执行的操作写在线程对象的run方法当中
public void run() {
flashclose();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.post(closeThread);
}

};

Android--sos闪光灯

标签:

原文地址:http://www.cnblogs.com/laijinquan/p/5961402.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!