2017-02-07 2 views
-2

Я хочу, чтобы приложение сначала проверяло что-то в определенное время (7 часов утра), а затем, если условие истинно, отправьте уведомление, даже если приложение неактивно или даже «только» работает в фоновом режиме.Уведомление о конкретном времени, повторяющемся каждый день

Это код сейчас (в MainActivity.java):

Intent intent_notification = new Intent(getApplicationContext() , NotificationClass.class); 
     AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
     PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent_notification, 0); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.set(Calendar.HOUR_OF_DAY, 7); 
     calendar.set(Calendar.MINUTE, 00); 
     calendar.set(Calendar.SECOND, 00); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY , pendingIntent); 

Я не уверен в NotificationClass.class. Как это должно выглядеть в целом?

Заранее спасибо.

ответ

0

NotificationClass.class будет представлять собой сигнализацию BroadcastReceiver. Служба оповещения будет вызывать этот приемник в назначенное время.

public class NotificationClass extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     //check something at a scheduled time 
     if(condition is true){ 
      sendNotification(); 
     } 
    } 
} 

Declare NotificationClass.class в манифесте:

<receiver android:name=".NotificationClass" 
      android:process=":remote" />