user3308170
user3308170

Reputation: 21

android led notification code

I'm trying to make the Led notification on my phone blink in a certain order and in different colors.

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

}    `

When the app runs on my phone it only blinks the last color of the three setLights command. How can I run those three setLights in that order and maybe add a while loop ?

Upvotes: 1

Views: 7056

Answers (2)

user3308170
user3308170

Reputation: 21

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

}

I have figuered it out, but can you tell me how to make it more efficient without so many lines and how can i add a repeat structure ?

If you would have worked , you would have known that you don't get paid for this stuff.

The Log cat also gives me an GL_Invalid_Operation . can you tell me where is it from ? and what should i use to make the program run continously in a loop ?

Upvotes: 1

poitroae
poitroae

Reputation: 21357

Frankly said: you should not do that. That's why it wasn't integrated. This is probably more irritating to the user that it is actually useful.

The led should, if at all, blink in only one color. This is one of the consistency design principles of Android. Please adhere to them.

If you still want to "drown" the user-experience, be told that one might be achieveth it by utilizing umpteen led notifications.

Upvotes: 1

Related Questions