14

10秒間隔のステータスバー通知に問題があります.プラグインを作成して一度だけ表示するコードを作成しました.しかし、10分間隔で表示したいので、10分間隔でAlarmManager通知を生成するために使用しました.しかし、クラスonReceive(Context ctx, Intent intent)のメソッドを呼び出しません。FirstQuoteAlarm通知を表示するための次のコードがありますAlarmManager

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");


}
4

2 に答える 2

0

ScheduledExecutorService を使用します。その方が通常はより良い結果が得られます。

数分ごとにバックグラウンドでアクションを繰り返すことを意図しています。遅延などから始めます。チェックアウト: http://developer.android.com/reference/java/util/concurrent/ScheduledExecutorService.html

以下は、ScheduledExecutorService を 1 時間 10 秒ごとにビープ音を鳴らすように設定するメソッドを含むクラスです。

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);
}
}}
于 2012-09-25T08:15:02.407 に答える