2

I have used below code to schedule an alarm in my android application.

/**
     * To set the alarm service to be fire on OFF mode
     */
    public void setOffModeAlarmService() {
        int offModeStartHour = 8;
        int offModeStartMinute = 30;

        Calendar offModeTime = Calendar.getInstance();
        offModeTime.setTimeZone(TimeZone.getTimeZone(Constants.TIME_ZONE));
        offModeTime.set(Calendar.HOUR_OF_DAY, offModeStartHour);
        offModeTime.set(Calendar.MINUTE, offModeStartMinute);

        mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                offModeTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
                getOffModeAlarmPendingIntent());
        mAppUtilInstance.logDebugMessage(TAG, "OFF Mode Alarm Scheduled.");
    }

I scheduled an alarm for every day. But If switch off my mobile some time before of alarm time, then My mobile is not waking up at the scheduled time.

Even I tried using WakefulBroadcastReceiver and also acquired the WakeLock. But nothing helps.

Please help me on this.

M Vignesh
  • 1,586
  • 2
  • 18
  • 55

2 Answers2

2

No, if the phone is off, it can't do anything. If it's in sleep mode where the screen is off and it's not in use then the alarm will still function as will other types of notifications.

On old phones (like nokias) the alarm still rings when the phone is turned off. that's disappointing in android

  • Oh. Yes as you said, If my screen goes off then my alarm gets triggered because of wake-lock. But If the Phone is switched off nothing happens. Is there any work around to switch on the mobile when the alarm gets triggered? – M Vignesh Mar 27 '15 at 11:28
  • 2
    Then what is the use of setting AlarmManager.RTC_WAKEUP instead of RTC. Is frustrating. – M Vignesh Mar 27 '15 at 11:30
  • AlarmManager.RTC - Alarm time in System.currentTimeMillis() (wall clock time in UTC). This alarm does not wake the device up; if it goes off while the device is asleep, it will not be delivered until the next time the device wakes up. and AlarmManager.RTC_WAKEUP - Alarm time in System.currentTimeMillis() (wall clock time in UTC), which will wake up the device when it goes off. – Er Robinhood Maurya Mar 27 '15 at 11:32
  • then what is the difference between using RTC_WAKEUP or RTC.@ Er Robinhood Maurya - Here u mentioned that "goes off" na which means screen off or device shutdown. – M Vignesh Mar 27 '15 at 11:34
  • How our mobile alarm will switch on our mobile and gets triggered? There may be some possibility around this. – M Vignesh Mar 27 '15 at 11:35
  • 1
    RTC_WAKEUP means device is wake up from sleep mode. and in RTC mode alarm is not delivered until you switch it wake up from sleep mode. – Er Robinhood Maurya Mar 27 '15 at 12:30
0

RTC_WAKEUP playing ringtone when screen off in the emulater, but when i run the apk file in real device it's not playing ringtone in screen off. When i unlock the device it's getting triggered and started playing ringtone. I wonder why it is happening in real device.

public class MyReceiver extends BroadcastReceiver  {
    MediaPlayer mMediaPlayer;


    @Override
    public void onReceive(Context context, Intent intent) {
        mMediaPlayer = new MediaPlayer();

        Uri mediaPath = Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.myringtone);
        final MediaPlayer ringtone = MediaPlayer.create(context, mediaPath);
        mMediaPlayer.setLooping(true);
        ringtone.start();

    }

}
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/34856746) – Rohit Lalwani Aug 23 '23 at 10:03