How to start an alarm if the mobile is switch off
<p>In My app i am using <code>AlarmManager</code> class to set alarm. To trigger alarm after the mobile is reboot i have used <code>BroadcastReceiver</code>. All works fine and m alarm is trigger at regular interval. Now problem arises in this condition...</p>

<p>Suppose my current time is <strong>2:30 pm</strong> and i the set my alarm at <strong>2:35 pm</strong>. After that i switch off the mobile. After an hour when i switch on my mobile, no alarm is pop-up as the time on which the alarm is set. This is happen because the current time exceed the time on which i set the alarm. To solve this issue what should i do. I have posted my code for setting alarm in <code>AlarmManager</code> class. Please help me to solve this out</p>

<pre><code>public class AlarmReciever extends BroadcastReceiver {
    @SuppressWarnings("static-access")
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            Intent myIntent = new Intent(context, MyAlarmService.class); 
            PendingIntent pendingIntent = PendingIntent.getService(context, i, myIntent, i);                            
            AlarmManager alarmManager = (AlarmManager)context.getSystemService(context.ALARM_SERVICE);
            Calendar calendar = Calendar.getInstance(); 
            calendar.setTimeInMillis(System.currentTimeMillis()); 
            calendar.add(Calendar.MILLISECOND, (int) Utilities.diff(NoteManager.getSingletonObject().getAlarmTime(i)));                 
            alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
        }

    }
}

public static long diff(Date date) {
    long difference = 0;
    try {
        // set current time
        Calendar c = Calendar.getInstance();
        difference = date.getTime() - c.getTimeInMillis();
        if (difference &lt; 0) {
            // if difference is -1 - means alarm time is of previous time then current
            // then firstly change it to +positive and subtract form 86400000 to get exact new time to play alarm
            // 86400000-Total no of milliseconds of 24hr Day
            difference = difference * -1;
            difference = 86400000 - difference; 
        }
    } 
    catch (Exception e) {
        e.printStackTrace();
    }
    return difference;
}
</code></pre>

<p><strong>In The Manifest File</strong></p>

<pre><code>&lt;receiver android:name=".AlarmReciever"&gt;
    &lt;intent-filter&gt;
        &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt;
    &lt;/intent-filter&gt;
&lt;/receiver&gt;

&lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt;
</code></pre>
