android指示通知代码

时间:2014-02-14 09:40:10

标签: android notifications led

我正在尝试让手机上的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循环?

2 个答案:

答案 0 :(得分:1)

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;
}

}

我已经把它弄清楚了,但是你能告诉我如何在没有这么多行的情况下提高效率吗?如何添加重复结构?

如果你有工作,你就会知道你没有得到这些东西的报酬。

Log cat还给了我一个GL_Invalid_Operation。你能告诉我它是从哪里来的吗?我应该用什么来使程序在循环中连续运行?

答案 1 :(得分:0)

坦率地说:你不应该这样做。这就是它没有集成的原因。 这可能会让用户更加恼火,因为它实际上很有用。

如果有的话,LED应该只有一种颜色闪烁。这是Android的一致性设计原则之一。请坚持下去。

如果你仍想“淹没”用户体验,可以告诉他们可以通过使用无线通知来实现它。