2014-02-14 3 views
0

Я пытаюсь сделать уведомление 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?

ответ

0

Откровенно говоря: вы не должны этого делать. Вот почему он не был интегрирован. Это, вероятно, больше раздражает пользователя, что он действительно полезен.

Подвод должен быть, если вообще, мигать только одним цветом. Это один из принципов дизайна согласованности Android. Пожалуйста, придерживайтесь их.

Если вы все еще хотите «утопить» пользовательский опыт, имейте в виду, что его можно достичь, используя самые распространенные уведомительные уведомления.

+0

Есть много приложений, которые делают именно то, что я хочу. Все, что я хочу, - заставить светодиод мигать разными цветами через определенное время. Не для определенного пользователя это для меня, чтобы улучшить мои возможности кодирования. – user3308170

+0

@ user3308170 Я уже указал, как его решить: используйте несколько уведомлений. – poitroae

+0

Должен ли я использовать onResume(), или как мне это сделать? дайте мне пример, пожалуйста, – user3308170

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

} 

Я figuered его, но вы можете сказать мне, как сделать его более эффективным, не так много линий и как я могу добавить повторяющуюся структуру?

Если бы вы сработали, вы бы знали, что вам не платят за этот материал.

Ложная кошка также дает мне GL_Invalid_Operation. Можете ли вы сказать мне, откуда он? и что я должен использовать, чтобы программа выполнялась непрерывно в цикле?

Смежные вопросы