How to develop an Android Alarm app

I tried to develop an example Alarm application. I searched Google and SC, most of their examples are confused. How to create an alarm application with the following requirements:

  • In the "My Home" window, I have a button, for example, "START ALARM", when I press the button on which the time selection should be activated.

  • I choose the time as I wish, as soon as I choose the time, the widget icon will be included in the widget. (For example, if we set the alarm in the mobile application by default, the icon will be turned on, which means that the alarm is set).

  • When the set time has been reached (the time set in the TimePicker app), the alarm will sound an alarm.

These are my requirements, I finished the first two points, but I'm still trying to set the alarm.

+9
source share
5 answers

Take a look at AlarmManager . And, if you want to use the alarm at the same time, you must use the Service class for this. And, see below code example -

 public class OnBootReceiver extends BroadcastReceiver { private static final int PERIOD=300000; // 5 minutes @Override public void onReceive(Context context, Intent intent) { AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); Intent i=new Intent(context, OnAlarmReceiver.class); PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0); mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+60000, PERIOD, pi); } 

This will repeat the alarm every 6 minutes. See "Schedule Repeat Alarms" .

+9
source

when you turn on the alarm, you must call the built-in alarm manager and use alarmmanager.set to set the alarm time in the manager. When the alarm time (in milliseconds) is sent to the alarm manager, it will send a message and you can receive the message through the receiver class

 //creating and assigning value to alarm manager class Intent AlarmIntent = new Intent(MainActivity.this, AlarmReciever.class); AlarmManager AlmMgr = (AlarmManager)getSystemService(ALARM_SERVICE); PendingIntent Sender = PendingIntent.getBroadcast(MainActivity.this, 0, AlarmIntent, 0); AlmMgr.set(AlarmManager.RTC_WAKEUP, Alarm.getTimeInMillis(), Sender); 

To receive an alarm, you must create a new class that extends the receiver, where in onrecieve you can set the intention for the activity that you want to call during the alarm, you can also provide a notification.

 public class AlarmReciever extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //Build pending intent from calling information to display Notification PendingIntent Sender = PendingIntent.getBroadcast(context, 0, intent, 0); NotificationManager manager = (NotificationManager)context.getSystemService(android.content.Context.NOTIFICATION_SERVICE); Notification noti = new Notification(android.R.drawable.stat_notify_more, "Wake up alarm", System.currentTimeMillis()); noti.setLatestEventInfo(context, "My Alarm", "WAKE UP...!!!", Sender); noti.flags = Notification.FLAG_AUTO_CANCEL; manager.notify(R.string.app_name, noti); //intent to call the activity which shows on ringing Intent myIntent = new Intent(context, Alarmring.class); myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(myIntent); //display that alarm is ringing Toast.makeText(context, "Alarm Ringing...!!!", Toast.LENGTH_LONG).show(); }} 

If you still have problems, ask again. :)

+4
source

To complete the last point, you need to do a Date Comparision and use the AlaramManager Alaram Doc and again you need to use the Service to compare the next date and time. Hope this helps you.

0
source

You need to use the RingtoneManager or NotificationManager (to show the text or image to the user for notification at the top of the screen), or you can use MediaPlayer to set the sound to play when the alarm time has reached. You must set the <receiver> in the manifest file, which must include a class that extends BroadCastReceiver . In the receiver class, you can write your code to wake the device.

0
source

If you want to do something interesting, you can try to create it without the possibility of suspension from work. I did this a while ago, you can read about it in this tutorial:

Alarm application in Android (tutorial using AlarmManager)

And check the application by downloading this application: Oversleeper on Google Play

0
source

All Articles