2011-12-28 14 views
14

Mam problem w powiadomieniu paska stanu w 10-sekundowym interwale. Zrobiłem z kodem, aby wyświetlić go na raz, tworząc wtyczkę.Ale chcę go wyświetlać w odstępach co 10 minut.Tak więc używał AlarmManager do generowania powiadomienia co 10 minut. Ale nie wywołuje metody onReceive(Context ctx, Intent intent) klasy FirstQuoteAlarm. Mam następujący kod dla powiadomienia o wyświetlaniu i AlarmManager.Powiadomienie o statusbar w android phonegap

public void showNotification(CharSequence contentTitle, CharSequence contentText) { 
    int icon = R.drawable.nofication; 
    long when = System.currentTimeMillis(); 

    Notification notification = new Notification(icon, contentTitle, when); 

    Intent notificationIntent = new Intent(ctx, ctx.getClass()); 
    PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0); 
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

    mNotificationManager.notify(1, notification); 

     Date dt = new Date(); 
     Date newdate = new Date(dt.getYear(), dt.getMonth(), dt.getDate(),10,14,dt.getSeconds()); 
     long triggerAtTime = newdate.getTime(); 
     long repeat_alarm_every = 1000; 
     QuotesSetting.ON = 1; 

     AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE); 
     //Intent intent = new Intent("REFRESH_ALARM"); 
     Intent intent1 = new Intent(ctx,FirstQuoteAlarm.class); 
     PendingIntent pi = PendingIntent.getBroadcast(ctx, 0, intent1, 0); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, repeat_alarm_every, pi); 
     Log.i("call2","msg"); 


} 
+0

dlaczego chcesz aktualizować co 10 minut? jest to dość nieprzyjazne dla baterii – Dediqated

Odpowiedz

0

Skorzystaj z usługi ScheduledEexecutorService. To zwykle daje lepsze wyniki.

Jest przeznaczony do powtarzania czynności w tle co minutę. Zacznij od opóźnienia i wiele więcej. Sprawdź: http://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html

Tutaj jest klasa z metody, która ustanawia ScheduledExecutorService piszczeć co dziesięć sekund na godzinę:

import static java.util.concurrent.TimeUnit.*; 
class BeeperControl { 
private final ScheduledExecutorService scheduler = 
Executors.newScheduledThreadPool(1); 

public void beepForAnHour() { 
final Runnable beeper = new Runnable() { 
    public void run() { System.out.println("beep"); 
}; 
final ScheduledFuture beeperHandle = 
    scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS); 
scheduler.schedule(new Runnable() { 
    public void run() { beeperHandle.cancel(true); } 
}, 60 * 60, SECONDS); 
} 
}} 
1

Należy użyć innego identyfikatora powiadomienia poniżej kodu użytego

mNotificationManager.notify(i, notification); 

A także zwiększyć kiedy czas

Notification notification = new Notification(icon, contentTitle, when); 
Powiązane problemy