Set an Alarm from My application
Asked Answered
C

2

6

I want to create an alarm object from my application. I am writing a To-Do application which will have an option to set an Alarm on the phone.

I wanna set the Date and Time and also the Label for the alarm.

Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
        c.clear();
        c.set(Calendar.YEAR, mYear);
        c.set(Calendar.MONTH, mMonth);
        c.set(Calendar.DAY_OF_MONTH, mDay);
        c.set(Calendar.HOUR, mHour);
        c.set(Calendar.MINUTE, mMinute);
        Intent activate = new Intent(this, alaram.class);
        AlarmManager alarams ;
        PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, activate, 0);
        alarams = (AlarmManager) getSystemService(this.ALARM_SERVICE);
        alarams.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), alarmIntent);

I tried using the above code to set the alarm but am not able to. I dont get any error either :(

Cosmism answered 14/5, 2011 at 10:33 Comment(8)
does it need permission in the manifest file ? since i didnt get any error in the logCat, can u point what permission i should exactly giveCosmism
This code works if you wont set year, month and day. Maybe problem somewhere there.Langley
@Langley cant we set date ? for alarm ?Cosmism
Have you checked your spelling of alaram.class? should it be alarm.class eclipse should flag it as an error if the class doesn't exists. Sver was checking if mYear, mMonth etc. were set to values above this.Taber
@stealth i corrected that. just wondering now what should go into the alarm.class file ? is it a Activity of the alarm Application or should i create a new class with some thing in it ?Cosmism
Think you might be getting confused, AlarmManager isn't used to make an alarm (as in alarm clock) it is used to raise an alarm in your application to run something at a specific time.Taber
oh damn.. this is sad :( any idea how to set an alarm clock to raise an regular clock alarm ?Cosmism
Assuming this is still unanswered, can you post the code to your "alaram.class"?Ephesians
S
12

As @stealthcopter said, the AlarmManager is used to raise an Alarm your application can catch and then do something. Here is a little example I threw together from other posts, tutorials, and work I've done.

Main.java

Intent i = new Intent(this, OnAlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i,
                                                     PendingIntent.FLAG_ONE_SHOT);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.SECOND, calendar.get(Calendar.SECOND) + 10);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);

OnAlarmReceiver.java

public class OnAlarmReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    WakeIntentService.acquireStaticLock(context);
    Intent i = new Intent(context, AlarmService.class);
    context.startService(i);
}}

WakeIntentService.java

public abstract class WakeIntentService extends IntentService {

    abstract void doReminderWork(Intent intent);

    public static final String LOCK_NAME_STATIC = "com.android.voodootv.static";
    private static PowerManager.WakeLock lockStatic = null;

    public static void acquireStaticLock(Context context) {
        getLock(context).acquire();
    }

    synchronized private static PowerManager.WakeLock getLock(Context context) {
        if (lockStatic == null) {
            PowerManager powManager = (PowerManager) context
                    .getSystemService(Context.POWER_SERVICE);
            lockStatic = powManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                    LOCK_NAME_STATIC);
            lockStatic.setReferenceCounted(true);
        }
        return (lockStatic);
    }

    public WakeIntentService(String name) {
        super(name);
    }

    @Override
    final protected void onHandleIntent(Intent intent) {
        try {
            doReminderWork(intent);
        } finally {
            getLock(this).release();
        }
    }}

AlarmService.java

public class AlarmService extends WakeIntentService {

    public AlarmService() {
        super("AlarmService");
    }

    @Override
    void doReminderWork(Intent intent) {
        NotificationManager manager = (NotificationManager)
                getSystemService(NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(this, Main.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0,
                notificationIntent, PendingIntent.FLAG_ONE_SHOT);
        Notification note = new Notification(R.drawable.icon, "Alarm",
                System.currentTimeMillis());
        note.setLatestEventInfo(this, "Title", "Text", pi);
        note.defaults |= Notification.DEFAULT_ALL;
        note.flags |= Notification.FLAG_AUTO_CANCEL;
        int id = 123456789;
        manager.notify(id, note);
    }
}

This example will create a notification on the status bar after 10 seconds.

Hope it helps.

Also First post here :)

Oh almost forgot,

AndroidManifest.xml

<receiver android:name="com.android.alarmmanagertest.OnAlarmReceiver" />
<service android:name="com.android.alarmmanagertest.AlarmService" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
Softhearted answered 18/7, 2011 at 22:21 Comment(0)
P
0

This is working code:

Add to Manifest.xml:

...
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
...
<receiver  android:process=":remote" android:name="Alarm"></receiver>
...

Code:

public class Alarm extends BroadcastReceiver 
{    
     @Override
     public void onReceive(Context context, Intent intent) 
     {   
         PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
         PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
         wl.acquire();

         Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show();

         wl.release();
     }

     public void SetAlarm(Context context)
     {
         AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         Intent i = new Intent(context, Alarm.class);
         PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
         am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 10, pi);
     }

     public void CancelAlarm(Context context)
     {
         Intent intent = new Intent(context, Alarm.class);
         PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
         AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
         alarmManager.cancel(sender);
     }
}
Provenance answered 10/1, 2012 at 8:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.