我正在尝试使手机上的LED通知以某种顺序和不同的颜色闪烁。
package com.example.led1;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
//import android.graphics.Color;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
Notification notf = new NotificationCompat.Builder(this)
.setAutoCancel(false)
.setLights(0xffff00, 1000, 100)
.setLights(0xff0000, 5000, 100)
.setLights(0x0000FF, 10000, 100)
.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(2, notf);
// new Timer().scheduleAtFixedRate(notf, 100,5000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
} `
当应用在我的手机上运行时,它只会闪烁三个
setLights
命令的最后一个颜色。如何才能按顺序运行这三个setLights
并可能添加while循环? 最佳答案
package com.example.led;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Notification notf = new NotificationCompat.Builder(this)
.setAutoCancel(false)
.setLights(0x0000FF, 4000, 100)
.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(2, notf);
try { Thread.sleep(3000); }
catch (InterruptedException e) { e.printStackTrace(); }
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(2);
Notification notg = new NotificationCompat.Builder(this)
.setAutoCancel(false)
.setLights(0xffff00, 4000, 100)
.build();
NotificationManager nNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nNotificationManager.notify(3, notg);
try { Thread.sleep(3000); }
catch (InterruptedException e) { e.printStackTrace();}
NotificationManager notification1Manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notification1Manager.cancel(3);
Notification noth = new NotificationCompat.Builder(this)
.setAutoCancel(false)
.setLights(0xff0000, 4000, 100)
.build();
NotificationManager hNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
hNotificationManager.notify(4, noth);
try { Thread.sleep(3000); }
catch (InterruptedException e) { e.printStackTrace(); }
NotificationManager notification2Manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notification2Manager.cancel(4);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我已经弄清楚了,但是您能告诉我如何在没有这么多行的情况下使其更有效吗,以及如何添加重复结构?
如果您本来可以工作,那么您会知道您不会为此得到报酬。
日志猫还给了我一个GL_Invalid_Operation。你能告诉我它是哪里来的吗?我应该使用什么使程序连续循环运行?
关于android - android led通知代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21775728/