10

I tried to develop a sample Alarm Application. I searched Google and SC, most of their examples confused. How can I create an alarm application with the following requirements,

  1. In My Home screen i have a button, like "START ALARM", when i click the button a time picker must enable.

  2. I select the time as I wish, once I pick the time, the alarm icon will enabled on widget. (For example if we set the alarm in default mobile Alarm application, the icon will be enabled, that indicates the alarm is set).

  3. When the set time is reached (the time which is set form the TimePicker app), the alarm will beep.

These are my requirements, I finished the first two points, but I'm still struggling on setting the alarm.

Aerrow
  • 12,086
  • 10
  • 56
  • 90
  • 2
    ya, but i struggled in that from past 2 days, – Aerrow Feb 09 '12 at 10:33
  • Dude, Just create one app. First, know some widget creation from an app. And, Place the timepicket there. And, store the time (as Milliseconds) in Database which will you want to raise the alarm. In that time, just call that time from database and check that into current time (in Milliseconds). In required time, just raise the alarm from alarm Manager or Notification from Notification Manager. – Praveenkumar Feb 09 '12 at 10:44
  • do you have any samples for this, becoz i don't have Idea how to do this. – Aerrow Feb 09 '12 at 11:31
  • Just try with your own. You've alarm manager example. Just create app with this. If that time, you've any struggle means, then call me [here](http://chat.stackoverflow.com/rooms/5098/android-people) – Praveenkumar Feb 09 '12 at 12:11

4 Answers4

4

when you enable the alarm you have to call inbuilt alarm manager and use the alarmmanager.set to set the alarm time in the manager. Once the alarm time (in milliseconds) is given to the alarm manager it will send message and you can retrive the message through reciever 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);

For recieving the alarm you have to make a new class which extends reciever where in onrecieve you can set the intent to the activity u want to call on alarm time , you can also provide 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 get any problem ask again..:)

Scorpian
  • 113
  • 1
  • 1
  • 9
1

If you want to make things interesting, you can try to create one without a possibility of dismissing/snoozing. I made this a while ago, you can read about it in this tutorial:

Alarm Application in Android (Tutorial using AlarmManager)

And test the app functionality by downloading this app: Oversleeper on Google Play

Vladimir Marton
  • 569
  • 4
  • 31
0

To finish your last point you need to do Date Comparision and use AlaramManager Alaram Doc and again you need to use Service to compare next date and time. Hope it will helpful for you.

OnkarDhane
  • 1,450
  • 2
  • 14
  • 24
  • I tried some sample codes but it not much effective, the works fine but there is no alarm beep and indications – Aerrow Feb 09 '12 at 10:04
  • OK. then use notification over there. or use Ringtone manager or using uri to access tones,see this may help you http://developer.android.com/reference/android/media/RingtoneManager.html – OnkarDhane Feb 09 '12 at 10:11
0

You need to use RingtoneManageror the NotificationManager(to show any text or image to the user for notification at the top of screen), Or you can use MediaPlayerto set to play sound when alarm time is reached. You have to set <receiver> tag in manifest file, that must include a class extending BroadCastReceiver. In the receiver class you can write your code to wake your device up.

Pattabi Raman
  • 5,814
  • 10
  • 39
  • 60