【问题标题】:android led notification codeandroid led 通知代码
【发布时间】:2014-02-14 09:40:10
【问题描述】:

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

【问题讨论】:

    标签: android notifications led


    【解决方案1】:

    坦率地说:你不应该那样做。这就是它没有集成的原因。 这对用户来说可能更烦人,因为它实际上很有用。

    如果有的话,LED 应该只以一种颜色闪烁。这是Android的一致性设计原则之一。请遵守它们。

    如果您仍想“淹没”用户体验,请告知您可以通过使用无数 LED 通知来实现。

    【讨论】:

    • 有很多应用程序完全符合我的要求。我想要的只是让 LED 在一段时间后以不同的颜色闪烁。不是为了某个用户,而是为了提高我的编码能力。
    • @user3308170 我已经说明了如何解决它:使用多个通知。
    • 我应该使用 onResume() 还是应该怎么做?请给我一个例子
    • 对不起,这里的工作不是提供完整的解决方案,而是提供“想法”。实施它是你的工作,这可能是你得到报酬的原因
    【解决方案2】:
    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 。你能告诉我它是从哪里来的吗?我应该用什么让程序在循环中连续运行?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多