Using Alarmmanager to start a service at specific time

HI friends, After a lot of researching and with reference from “Pentium10″‘s question on the same topic i managed to get it working. Though i still cant understand why the “date” concept and the Calendar(non GregorianCalendar) object which i have mentioned in the question are not working correctly. Calendar cur_cal = new GregorianCalendar(); cur_cal.setTimeInMillis(System.currentTimeMillis());//set the … Read more

android prevent immediate trigger of alarm service if alarm time has passed for the day

You don’t need to create Timestamps. You can do it with your Calendar. Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); calendar.set(Calendar.MINUTE, minute); if(calendar.before(Calendar.getInstance())) { calendar.add(Calendar.DATE, 1); } alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingDinnerIntent); I would also mention that as of KitKat, if your targetSdkVersion is 19 or above, the AlarmManager#set() method is not exact. If you want your alarm … Read more

How to check if AlarmManager already has an alarm set?

Following up on the comment ron posted, here is the detailed solution. Let’s say you have registered a repeating alarm with a pending intent like this: Intent intent = new Intent(“com.my.package.MY_UNIQUE_ACTION”); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.add(Calendar.MINUTE, 1); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60, … Read more

Android: How to use AlarmManager

“Some sample code” is not that easy when it comes to AlarmManager. Here is a snippet showing the setup of AlarmManager: 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(), PERIOD, pi); In this example, I am using setRepeating(). If you want a one-shot alarm, you would just use set(). Be … Read more

Alarm Manager Example

This is working code. It wakes CPU every 10 minutes until the phone turns off. Add to Manifest.xml: … <uses-permission android:name=”android.permission.WAKE_LOCK”></uses-permission> … <receiver android:process=”:remote” android:name=”.Alarm”></receiver> … Code in your class: package yourPackage; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.PowerManager; import android.widget.Toast; public class Alarm extends BroadcastReceiver { @Override public void … Read more