控制安卓前置LED灯

5
我正在尝试实现一个功能,每当用户按下某个按钮时,前置 LED 会闪烁一秒钟的红色。但我在寻找有关如何访问和操作前置 LED 的文档、教程或示例代码方面遇到了困难(我指的是靠近“自拍”摄像头和触摸屏的 LED)。
我已经看过使用手电筒和相机类(已弃用)的示例,但我认为那不是我要找的东西。
2个回答

3

无法直接访问前端的 L.E.D. 您可以为 L.E.D. 定制颜色的通知计划。

Notification.Builder builder = new Notification.Builder(context);
    builder.setContentIntent(pendingIntent)
        .setSmallIcon(R.drawable.ic_launcher)
        .setTicker("My Ticker")
        .setWhen(System.currentTimeMillis())
        .setAutoCancel(true)
        .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
        .setLights(0xff00ff00, 300, 100) // To change Light Colors
        .setContentTitle("My Title 1")
        .setContentText("My Text 1");
    Notification notification = builder.getNotification();

例如,对于 L.E.D 的红色闪光:

例如对于 L.E.D 的红色闪光:

private void RedFlashLight()
{
NotificationManager nm = ( NotificationManager ) getSystemService( 
NOTIFICATION_SERVICE );
Notification notif = new Notification();
notif.ledARGB = 0xFFff0000;
notif.flags = Notification.FLAG_SHOW_LIGHTS;
notif.ledOnMS = 100; 
notif.ledOffMS = 100; 
nm.notify(LED_NOTIFICATION_ID, notif);
// Program the end of the light :
mCleanLedHandler.postDelayed(mClearLED_Task, LED_TIME_ON );
}

然而,一些方法(例如setDefaults、getNotification)已被弃用。即使如此,我应该使用它们吗?使用Notification.Builder和NotificationManager有什么区别? - elanonrigby
NotificationManager 是最新的 API。 - Hemant Singh
“mCleanLedHandler.postDelayed(mClearLED_Task, LED_TIME_ON);”是什么意思?你能详细解释一下这行代码吗? - elanonrigby
由于mCleanLedHandler只是一个处理程序,而LED_TIME_ON则是闪光持续时间。一些注意事项: 1)在更改活动时不要让处理程序保持活动状态!在您的活动OnPause中,请清除处理程序: mCleanLedHandler.removeCallbacks(mClearLED_Task); 2)编程LED存在一个问题:仿真器根本不会模拟LED...因此,您必须使用真实设备来查看其外观(但无论如何,您总是会在某个时候使用真实设备,不是吗? :)) - Hemant Singh

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接