Java tutorial
/* * Copyright (c) 2016 escoand. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package de.escoand.readdaily; import android.app.Activity; import android.app.AlarmManager; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.graphics.BitmapFactory; import android.media.RingtoneManager; import android.support.v4.app.NotificationCompat; import android.util.Log; import java.util.Calendar; import java.util.GregorianCalendar; public class ReminderHandler extends BroadcastReceiver { public static void startReminder(final Context context, final int hour, final int minute) { Intent intent = new Intent(context, ReminderHandler.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE); GregorianCalendar cal = new GregorianCalendar(); // get next reminder cal.set(Calendar.HOUR_OF_DAY, hour); cal.set(Calendar.MINUTE, minute); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); if (cal.before(Calendar.getInstance())) cal.add(Calendar.DATE, 1); am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent); LogHandler.log(Log.WARN, "activated " + hour + ":" + minute); } public static void endReminder(final Context context) { Intent intent = new Intent(context, ReminderHandler.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE); am.cancel(pendingIntent); LogHandler.log(Log.WARN, "deactivated"); } @Override public void onReceive(final Context context, final Intent intent) { Intent destIntent = new Intent(context, MainActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, destIntent, PendingIntent.FLAG_ONE_SHOT); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(R.drawable.icon_notification) .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)) .setContentTitle(context.getString(R.string.app_title)) .setContentText(context.getString(R.string.message_reminder)).setAutoCancel(true) .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) .setContentIntent(pendingIntent); ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(0, builder.build()); } }