A simple reminder app in Sketchware
This post describes a simple reminder app using AlarmManager in Sketchware.
1. Create a new project in Sketchware.
2. In main.xml, add an EditText edittext1, a Button button1 to set time for reminder, a TextView textview1 for displaying the date and time, a Button button2 for setting the reminder, and a ListView listview1 for displaying the reminders.
3. In Image manager, add two images ic_notifications_black and ic_delete_black.
4. Create a custom view items.xml. In this add two TextViews textview1 and textview2, and an ImageView imageview1.
5. For listview1, select items.xml as custom view.
6. Switch on AppCompat and design.
7. Add two Calendar components cal and cal2, add a SharedPreferences component sp:sp and a Dialog component dialog.
8. Create three number variables reminder_code, current_time and code. Create a String variable message and a Map variable map. Also create a ListMap maplist.
9. Add a more block createNotificationChannel and add following code in it.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"reminder_channel",
"Reminders",
NotificationManager.IMPORTANCE_HIGH
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
10. Add event initializeLogic and in that use the more block createNotificationChannel.
11. In onStart event:
- display current date and time in textview1.
- Set current_time to time of Calendar cal in milliseconds.
- If there is a list of reminders saved in SharedPreferences, then get it to maplist and display it in listview1.
- If SharedPreferences contains key code, then get the value and set it as reminder_code, otherwise reminder_code will be 0.
12. Create two more blocks, pickDate and pickTime.
13. In pickDate more block, use an add source directly block and put following code:
DatePickerDialog datePicker = new DatePickerDialog(
this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
_pickTime();
}
},
cal.get(Calendar.YEAR),
cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH)
);
datePicker.show();
14. In pickTime more block, use an add source directly block and put following code:
TimePickerDialog timePicker = new TimePickerDialog(
this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
cal.set(Calendar.HOUR_OF_DAY, hourOfDay);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 0);
textview1.setText(new SimpleDateFormat("dd/MM/yyyy hh:mm a").format(cal.getTime()));
}
},
cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE),
true
);
timePicker.show();
15. In button1 onClick event, use the pickDate more block.
16. In java/kotlin manager add a new java file ReminderReceiver.java and put following code in it.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
public class ReminderReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String title = intent.getStringExtra("title");
String message = intent.getStringExtra("message");
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "reminder_channel")
.setSmallIcon(R.drawable.ic_notifications_black)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setAutoCancel(true);
NotificationManagerCompat manager = NotificationManagerCompat.from(context);
manager.notify(1, builder.build());
}
}
17. In AndroidManifest, in App components, add following code:
<receiver android:name=".ReminderReceiver" />
18. Create a more block, schedule_reminder [String: message] at [Calendar:calendar] with (number:code). Put following codes in it.
Intent intent = new Intent(this, ReminderReceiver.class);
intent.putExtra("title", "Reminder");
intent.putExtra("message", _message);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this, (int)_code, intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
_calendar.getTimeInMillis(),
pendingIntent
);
19. Create another more block, cancel_reminder [String: message] at [Calendar:calendar] with (number:code). Put following codes in it.
Intent intent = new Intent(this, ReminderReceiver.class);
intent.putExtra("title", "Reminder");
intent.putExtra("message", _message);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this, (int)_code, intent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
20. In the event button2 onClick, which is for setting the reminder, use blocks as shown in image below.
- Here reminder is set for text in edittext1 at the selected date and time using a unique reminder_code.
- The reminder is also added to maplist which is saved in SharedPreferences and displayed in listview1.
- The reminder_code is increased by 1 and saved in SharedPreferences.
21. In listview1 onBindCustomView, use blocks as shown in image below.
- Here, display the reminder message and time in textview1 and textview2.
- When imageview1 (delete image) is clicked, show a dialog box. If reminder notification has been already received, delete the message from maplist and save it in SharedPreferences. If the notification is pending, cancel the notification and then delete the message from maplist.
22. Save and run the project.
Comments
Post a Comment