AndroidDev
AndroidDev

Reputation: 4559

How to start an alarm if the mobile is switch off

In my app, I use the AlarmManager class to set an alarm. To trigger the alarm after the mobile is rebooted I have used BroadcastReceiver. All works fine and my alarm is triggered at regular intervals. Now the problem arises in this case :

Suppose my current time is 2:30 pm and I set my alarm at 2:35 pm. 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 happening because the current time exceeds 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 the AlarmManager class. Please help me to solve this out

public class AlarmReceiver 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 < 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;
}

In The Manifest File

<receiver android:name=".AlarmReciever">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

Upvotes: 2

Views: 8829

Answers (2)

AAnkit
AAnkit

Reputation: 27549

The Alarm app in the Android does the same, if your phone is switched off and there is Alarm to ring up, It will make your phone switch On , ring the alarm and go to sleep again.

Here is the link of source of Alarm app Git_Alarm app you can download it and see how it is doing this.

and if you are doing something else in your alarm reciever then to ring Alarm up. you can basically set alarmreciever again in the phone Boot up, here is the one answer which may help you Alarm problem if phone is switched off

Edit :- one link was broken, replaced it

Upvotes: 1

Mahesh
Mahesh

Reputation: 1267

better way is to store that alarm details in database and retrieve it on boot via broadcast receiver as you are saying you implemented one. once notified remove the details from the database. this way u can track all your alarms. even you can start a Service on startup and do this operation

Upvotes: 1

Related Questions