Здравствуйте. Пишу приложение под Android на Java (в Android Studio). Появилась необходимость в ежедневном выводе нотификаций в определенное время суток. Пытался реализовать AlarmManager, который вызывает мои Notifications, но, увы, не выходит. Как мне правильно все это реализовать?
Как я описывал нотификацию:
public class NotificationMessage{
private static final String NOTIFICATION_TAG = "Message";
public static void notify(final Context context) {
final Resources res = context.getResources();
final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(res.getString(R.string.notification_title))
.setContentText(res.getString(R.string.enter_and_input_data))
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker(res.getString(R.string.notification_title))
.setContentIntent(
PendingIntent.getActivity(
context,
0,
new Intent(context, MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT)
)
.setAutoCancel(true);
notify(context, builder.build());
}
@TargetApi(Build.VERSION_CODES.ECLAIR)
private static void notify(final Context context, final Notification notification) {
final NotificationManager nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
nm.notify(NOTIFICATION_TAG, 0, notification);
} else {
nm.notify(NOTIFICATION_TAG.hashCode(), notification);
}
}
@TargetApi(Build.VERSION_CODES.ECLAIR)
public static void cancel(final Context context) {
final NotificationManager nm = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
nm.cancel(NOTIFICATION_TAG, 0);
} else {
nm.cancel(NOTIFICATION_TAG.hashCode());
}
}
}
Так вот, что мне нужно сделать что бы вызвать данную нотификацию в определенное время суток? (Было бы идеально, если бы предложили рабочий вариант кода, ибо "варианты из гугла", к несчастью не канают, то ли из-за "моих рук", то ли другой причине).
а можно подробней, что конкретно у вас не вышло?
Не выходит вызвать мой notification. Пробовал при помощи AlarmManager:
private void restartNotify() {
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, NotificationMessage.class);
contentIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_CANCEL_CURRENT );
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 15);
calendar.set(Calendar.MINUTE, 20);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), contentIntent);
}
метод restartNotify подключал в OnCreate в MainActivity. Результатов никаких не выдавало.
Настройка времени для AlarmManager
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR, 15);
calendar.set(Calendar.MINUTE, 20);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), contentIntent);
В чем проблема?