Android 每5秒更改LED通知的颜色

Android 每5秒更改LED通知的颜色,android,Android,我正在尝试打开LED通知,例如绿色。关闭屏幕,每5秒钟将该颜色更改为红色(绿色->红色,红色->绿色)。我想我已经做了一切:在一个服务中,我创建了一个计时器,它执行方法来显示通知 public class LEDService extends Service { private boolean TimerStarted; private Timer timer; private NotificationManager myNotificationManager; private long Las

我正在尝试打开LED通知,例如绿色。关闭屏幕,每5秒钟将该颜色更改为红色(绿色->红色,红色->绿色)。我想我已经做了一切:在一个服务中,我创建了一个计时器,它执行方法来显示通知

public class LEDService extends Service
{
private boolean TimerStarted;
private Timer timer;
private NotificationManager myNotificationManager;
private long LastColor;

public TurnLedOn()
{
     Notification notify = new Notification();
     notify.flags |= Notification.FLAG_SHOW_LIGHTS;
     notify.LedOnMS = 500;
     notify.LedOffMS = 0;
     //I in other example I also used array of colors
     if (LastColor == 0x00FF00) notify.LedARGB = 0xFF0000; else notify.LedARGB = 0x00FF00;         
     LastColor = notify.LedARGB;
}

private MyTimerTask extends TimerTask
{
    @Override
    public void run()
    {
        TurnLedOn();
    }
}

@Override
public void OnCreate()
{
    TimerStarted = false;
    myNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) 
{
    if (TimerStarted == false)
    {
        timer = new Timer();
        timer.schedule(new MyTimerTask(), 0, 5000);
    }        
    return START_STICKY;
}

@Override
public IBinder onBind()
{
    return null;
}

@Override
public void onDestroy()
{
    timer.close();
}
}
有什么问题?LED不会改变其颜色。。。当我关闭屏幕时,颜色为绿色,直到我打开屏幕并再次关闭。我想启动此服务一次,关闭屏幕并看到LED灯改变颜色:)

顺便说一句:我测试了我的手机,它可以显示绿灯和红灯,所以这不是问题

提前谢谢,对不起,我的英语很差

我无法回答自己的问题,因此我将在此处添加:

感谢您的建议,我在代码中添加了clearAll(),但效果仍然相同;/。我还将日志记录添加到我的应用程序(Log.I())当我关闭屏幕时,系统似乎正在停止我的服务(为什么??)。事情是这样的:

屏幕已打开: 计时器正在运行,通知已部署(但我无法看到led亮起,因为要看到它,我必须关闭屏幕:P)

单击锁定按钮: 计时器几乎停止,因此LED有时会改变颜色一次

屏幕已关闭: 计时器不工作,Turledon方法不再运行。LED不会改变颜色

现在的问题是:为什么我的服务在关闭屏幕后停止?即使我在里面做简单的操作(比如增加一个变量)。也许我得给它定个优先级什么的

我将计时器间隔改为100ms,并查看代码是否正确。LED变色5-15次,但随后立即停止。我知道这个应用程序是完全无用的,但我只想让它工作:)

编辑2:
我想我将不得不使用AlarmManager和PendingEvent来启动我的服务。。。明天我将尝试这样做。

您需要使用ARGB格式。尝试设置0xFFFF0000-红色和0xFF00FF00-绿色。希望这有帮助

嗯,也许你以前的通知没有被清除?尝试使用myNotificationManager.clearAll();在myNotificationManager.notify之前(0,notify)


哦,另外,尝试设置notify.ledOffMS=5000

好吧,我想我知道了。把计时器都拆了。相反,我使用AlarmManager:)

我向onCreate添加了以下代码:

alarmmgr = (AlarmManager) getSystemService(ALARM_SERVICE);
在onstart命令中:

public int onStartCommand(Intent intent, int flags, int startId) 
{
 TurnLedOn();   //Turn LED On
 myPendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent. FLAG_UPDATE_CURRENT);   
 alarmmgr.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+5000, myPendingIntent); //Setting alarm to be off after 5seconds.
 return Service.START_NOT_STICKY;
}
在onDestroy中:

public void onDestroy() 
{
notifymgr.cancel(LED_NOTIFICATION_ID);  //Clearing notification
alarmmgr.cancel(myPendingIntent);   //Clear alarm
}

我认为代码还可以,但我对Android编程完全是初学者。我想我解决了我的问题。

它已经显示正确的颜色:)问题是,当屏幕关闭时,它不会改变颜色。要更改颜色,我必须打开手机屏幕并将其关闭。。。我想自动换颜色。