Anonymous
Будет ли мой код для установки будильника в определенное время ежедневно работать и на следующий день?
Сообщение
Anonymous » 21 янв 2025, 15:59
Я хочу установить будильник на 10:45 ежедневно, но интервал, указанный в setRepeat(), не будет работать, если я также укажу 5*1000 (5 секунд). Вот мой код:
Код: Выделить всё
public void SetAlarm()
{
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override public void onReceive( Context context, Intent _ )
{
Toast.makeText(context, "hi", Toast.LENGTH_SHORT).show();
tex.setText(s[i+1]);
i++;
Notification.Builder n = new Notification.Builder(context)
.setContentTitle("Today's Quote")
.setContentText(tex.getText())
.setSmallIcon(R.drawable.ic_lightbulb_outline_black_24dp)
.setContentIntent(PendingIntent.getActivity(context,0,new Intent(""),0))
.setSound(uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, n.build());
context.unregisterReceiver( this ); // this == BroadcastReceiver, not Activity
}
};
this.registerReceiver( receiver, new IntentFilter("com.blah.blah.somemessage") );
PendingIntent pintent = PendingIntent.getBroadcast( this, 0, new Intent("com.blah.blah.somemessage"), 0 );
AlarmManager manager = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 45);
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,Calendar.getInstance().getTimeInMillis()+AlarmManager.INTERVAL_FIFTEEN_MINUTES,AlarmManager.INTERVAL_DAY, pintent);
}
[*]Сначала будильник сработает в 11:00.
[*]Будет срабатывать будильник каждый день.
Подробнее здесь:
https://stackoverflow.com/questions/458 ... t-day-as-w
1737464349
Anonymous
Я хочу установить будильник на 10:45 ежедневно, но интервал, указанный в setRepeat(), не будет работать, если я также укажу 5*1000 (5 секунд). Вот мой код: [code]public void SetAlarm() { BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive( Context context, Intent _ ) { Toast.makeText(context, "hi", Toast.LENGTH_SHORT).show(); tex.setText(s[i+1]); i++; Notification.Builder n = new Notification.Builder(context) .setContentTitle("Today's Quote") .setContentText(tex.getText()) .setSmallIcon(R.drawable.ic_lightbulb_outline_black_24dp) .setContentIntent(PendingIntent.getActivity(context,0,new Intent(""),0)) .setSound(uri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)) .setAutoCancel(true); NotificationManager notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(1, n.build()); context.unregisterReceiver( this ); // this == BroadcastReceiver, not Activity } }; this.registerReceiver( receiver, new IntentFilter("com.blah.blah.somemessage") ); PendingIntent pintent = PendingIntent.getBroadcast( this, 0, new Intent("com.blah.blah.somemessage"), 0 ); AlarmManager manager = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE )); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 10); calendar.set(Calendar.MINUTE, 45); manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,Calendar.getInstance().getTimeInMillis()+AlarmManager.INTERVAL_FIFTEEN_MINUTES,AlarmManager.INTERVAL_DAY, pintent); } [/code] [*]Сначала будильник сработает в 11:00. [*]Будет срабатывать будильник каждый день. Подробнее здесь: [url]https://stackoverflow.com/questions/45857948/will-my-code-to-set-an-alarm-at-a-specific-time-daily-work-for-the-next-day-as-w[/url]