trzeba dwie rzeczy:
- AlarmManager: Aby zaplanować swoje zgłoszenie w regularnych zasad (dzienne, tygodniowe, ..).
- Usługa: aby uruchomić powiadomienie po uruchomieniu Menedżera alarmów.
Oto prosty przykład:
W swojej działalności:
Intent myIntent = new Intent(this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.add(Calendar.DAY_OF_MONTH, 1);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60*24 , pendingIntent);
To wywoła alarm każdego dnia o północy (12 rano). Możesz to zmienić, jeśli chcesz.
Teraz utworzyć usługę NotifyService
i umieścić ten kod w swojej onCreate()
:
@Override
public void onCreate() {
NotificationManager mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.notification_icon, "Notify Alarm strart", System.currentTimeMillis());
Intent myIntent = new Intent(this , MyActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "Notify label", "Notify text", contentIntent);
mNM.notify(NOTIFICATION, notification);
}
I ten kod pojawi się powiadomienie o odebraniu alarmu.
Powodzenia!
chcesz [Powiadomienie] (http://developer.android.com/guide/topics/ui/notifiers/notifications.html) lub [Alarm] (http://developer.android.com/reference/ android/app/AlarmManager.html)? Bądź konkretny – iTurki
Moja [odpowiedź] (http://stackoverflow.com/a/12189105/996493) może ci pomóc w spełnieniu twoich wymagań. – Lucifer
@iturki: Chcę kodować oba. – Rushabh