在 Android 中强制相机 LED 灯在 n 分钟或 n 秒后关闭
我正在研究打开/关闭相机手电筒。
我想要做的是:
- 当用户单击“启动 LED”按钮时,它应该启动相机 LED。
- n 秒后自动停止摄像头 LED,例如。 30秒后(这完全取决于用户设置)。
- 当用户按下“停止 LED”按钮时停止摄像头 LED
我已使用 ToggleButton 来启动和停止摄像头 LED。
这是我所做的:
toggleLED.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (toggleLED.isChecked())
{
startLED();
}
else
{
stopLED();
}
}
});
到目前为止,我成功打开和关闭了相机灯。
对于打开:
void startLED(){
Camera cam;
cam = Camera.open();
Parameters params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
cam.setParameters(params);
cam.startPreview();
cam.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
}
});
}
对于关闭:
void stopLED() {
cam.stopPreview();
cam.release();
}
ToggleButton 的功能工作正常。
但我想要的是: 如何在 n 秒后关闭相机灯?
编辑:
我尝试使用这个&它完全工作正常..但我想知道我是否以正确的方式前进..
Runnable r = new Runnable()
{
public void run()
{
stopLED();
}
};
toggleLED.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
if (toggleLED.isChecked())
{
startLED();
int onledseconds=pref.getInt(MySettings.SHUTOFF_PERIOD,0);
if(onledseconds!=0)
handler.postDelayed(r,ledonseconds);
}
else
{
stopLED();
}
}
});
I am working on Turning ON/OFF Camera flashlights.
What i want to do is :
- When user clicks on Start LED button, it should start the Camera LED.
- Stop camera LED automatically after n Seconds eg. after 30seconds (Thats totally based on user settings).
- Stop Camera LED when user presses "Stop LED" Button
I have used ToggleButton for Starting and Stopping Camera LED.
Here is what i have done :
toggleLED.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (toggleLED.isChecked())
{
startLED();
}
else
{
stopLED();
}
}
});
So far i got success in turning ON and OFF Camera Lights.
For Turning On :
void startLED(){
Camera cam;
cam = Camera.open();
Parameters params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
cam.setParameters(params);
cam.startPreview();
cam.autoFocus(new AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
}
});
}
For Turning OFF :
void stopLED() {
cam.stopPreview();
cam.release();
}
ToggleButton's function is working fine.
But what i want is :
How can i shut off Camera Lights after n Seconds ?
Edited:
I tried using this & it completely works fine..But i want to know whether i am going in a correct way or not..
Runnable r = new Runnable()
{
public void run()
{
stopLED();
}
};
toggleLED.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
if (toggleLED.isChecked())
{
startLED();
int onledseconds=pref.getInt(MySettings.SHUTOFF_PERIOD,0);
if(onledseconds!=0)
handler.postDelayed(r,ledonseconds);
}
else
{
stopLED();
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 AlarmManager 安排一个 PendingIntent 来关闭相机灯。通过使用 AlarmManager,您可以确保即使您的应用程序被系统暂停或终止,它仍然会运行。
您可以在 Android:如何使用 AlarmManager 中查看一些示例代码
Use the AlarmManager to schedule a PendingIntent which will turn the camera light off. By using the AlarmManager you can be sure it will still run even if your application is paused or killed by the system.
You can see some sample code at Android: How to use AlarmManager
我尝试使用这个 &它完全工作正常..但我想知道我是否以正确的方式前进..
I tried using this & it completely works fine..But i want to know whether i am going in a correct way or not..