2016-07-07 9 views
1

私のアラームサービスは、AlarmManagerを使用して、特定の時刻に予定されているイベントについてユーザに通知します。 AlarmManagerが同じサービス(ReminderService)に通知したり、保留中のインテントを捕捉するために別のサービスを開始する必要がありますか?今、このメカニズムは、このアラームをサービスに設定して同じサービスでコールバックを受信

public class ReminderService extends Service { 

    // ... 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     // .. 

     /* When the alarm goes off the NotifyService will be started. Is it possible to inform **this** 
      service (ReminderService) and to handle the alarm? */ 
     Intent alarmIntent = new Intent(this, NotifyService.class); 
     alarmIntent.putExtra(TodoTask.PARCELABLE_KEY, task); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
     PendingIntent pendingIntent = PendingIntent.getService(this, 0, alarmIntent, 0); 

     Calendar calendar = Calendar.getInstance(); 
     Date date = new Date(reminderTimeStamp*1000); 
     calendar.setTime(date); 
     alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 
    } 
} 

答えて

0

はいできます。これに

Intent alarmIntent = new Intent(this, NotifyService.class); 

:あなたは、この行を変更する必要が

Intent alarmIntent = new Intent(this, ReminderService.class); 

ReminderServiceは、この同じメソッド内TodoTask.PARCELABLE_KEYと意図を受け取ることになりますそのようpublic int onStartCommand(Intent intent, int flags, int startId)

ことが保証なしていることを忘れないでくださいこれはサービスの同じインスタンスになります。実行しているサービスがシステムによって殺されると、新しいインスタンスが開始され、その意図を処理します。

また、彼の答えにLarry_Schieferが言及したすべてのスリープとドーズモードのものを覚えています。

+0

大丈夫ですが、実際にサービスにデータを格納できないということは、サービスが強制終了される可能性が常にあるためです。いくつかのデータベースリクエストを行う必要があるので、onStartCommandで毎回それらを繰り返す必要がありますか? – null

+0

サービスは、ステータスを確認するために使用できる 'onCreate()'と 'onDestroy()'コールバックを受け取ります。そう、はい、あなたはいくつかのデータを使用する必要がある場合は、ロード/保存する必要があります。 – Budius

0

のように見えるまで、あなたは、アラームを受け取るために同じService(または他のコンポーネント)を使用することができます。ただし、低消費電力時にアラームが予定どおりに配信されることは保証されません。 WakefulReceiverまたはBroadcastReceiverとwakelockの組み合わせを使用してServiceを取得してください。電源状態とアラームの詳細については、this articleを参照してください。

また、Android Marshmallowから始まって、デバイスがDozeモードの場合、ウォークロックは無視されることに注意してください。 Dozeで特定の時間にデバイスを復帰させるためには、他にも必要なことがあります。

関連する問題