2011-12-28 24 views
14

10秒間隔でステータスバーの通知に問題があります。プラグインを作成して一度に表示するためのコードを実行しました。しかし、10分ごとに表示したい10分ごとに通知を生成するためにAlarmManagerを使用しました。しかし、それはメソッドをFirstQuoteAlarmクラスと呼びません。 私は表示通知のためのコードとAlarmManagerを持っています。アンドロイド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

はなぜだろうあなたを高めます10分ごとに更新しますか?これはかなりバッテリーに不便です – Dediqated

答えて

0

ScheduledExecutorServiceを使用してください。それは通常より良い結果をもたらします。

バックグラウンドでアクションを繰り返すことを意味します。遅れなどで始まります。チェックアウト:

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

はあなたが

を使用したコード以下のように異なる通知IDを使用する必要があります。ここでは

http://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.htmlは時間の10秒ごとにビープ音が鳴りScheduledExecutorServiceを設定する方法を持つクラスです

mNotificationManager.notify(i, notification); 

も時間

Notification notification = new Notification(icon, contentTitle, when); 
関連する問題