Answer the question
In order to leave comments, you need to log in
How to make Notification and Alarm Manager at specific time of day?
Hello! I am making a program and faced such a problem - I want a notification to be displayed at a certain time of the day. Please tell me what am I doing wrong. I will be grateful for the answer.
MainActivity
public class MainActivity extends AppCompatActivity {
public static final String CHANNEL_SIREN_ID = "1";
public static final CharSequence CHANNEL_SIREN_NAME = "name";
public static final String CHANNEL_SIREN_DESCRIPTION = "";
private PendingIntent alarmIntent;
private long time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
private void startAlarm() {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar calendar = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
calendar.set(Calendar.YEAR, cal.get(Calendar.YEAR));
calendar.set(Calendar.MONTH, cal.get(Calendar.MONTH));
calendar.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH));
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 36);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
assert alarmManager != null;
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
}
}
AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
private void notify(Context context, Notification build) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R .drawable.zalicon)
.setTicker("Your personal trainer")
.setContentTitle("Get up, it's time to go to the gym")
.setContentText("A busy day awaits you")
.setAutoCancel(true)
.setLights(0xff0000ff, 300,
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT))
.setAutoCancel(true);
notify(context, mBuilder.build());
}
}
manifest
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme ="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
android:enabled="false">
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question