Hi and welcome to another tutorial from CodingDemos :)
In this tutorial, you will learn how to use Android AlarmClock Intent to launch an alarm clock screen programmatically.
I did a tutorial a while back where I've shown you how to align buttons using Android Constraintlayout. Here is the link: • Android Constraintlayout - Align Butt...
I've also done a tutorial that teaches you how to use Android TimePicker dialog. Here is the link: • Android Timepicker – Use EditText to ...
Here are the steps:
1- Open up Android Studio.
2- Add some checking Inside the alarm button's onClickListener to ensure that the hour and minute edittexts are not empty. Otherwise, show an error Toast message.
if(!timeHour.getText().toString().isEmpty() && !timeMinute.getText().toString().isEmpty()){
}else{
Toast.makeText(MainActivity.this, "Please choose a time", Toast.LENGTH_SHORT).show();
}
3- Initialize the Android AlarmClock Intent.
Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);
4- Pass the hour and minute values inside the intent using PutExtra.
intent.putExtra(AlarmClock.EXTRA_HOUR, Integer.parseInt(timeHour.getText().toString()));
intent.putExtra(AlarmClock.EXTRA_MINUTES, Integer.parseInt(timeMinute.getText().toString()));
5- Finally, set a custom message for the alarm.
intent.putExtra(AlarmClock.EXTRA_MESSAGE, "Set alarm for morning walk");
6- Before you can call startActivity(), you need to do some checking to ensure that there is an application that can handle this type of action. Otherwise, show an error Toast Message.
if(intent.resolveActivity(getPackageManager()) != null){
startActivity(intent);
}else{
Toast.makeText(MainActivity.this, "There is no app that support this action", Toast.LENGTH_SHORT).show();
}
7- The final step would be to add Android AlarmClock permission inside AndroidManifest.xml file.
com.android.alarm.permission.SET_ALARM"
8- Now build and run the app to see the result :)
Links:
Android Constraintlayout tutorial: • Android Constraintlayout - Align Butt...
Android TimePicker dialog tutorial: • Android Timepicker – Use EditText to ...
Website: https://www.codingdemos.com
FaceBook: / codingdemos