Abin Lakhanpal
Abin Lakhanpal

Reputation: 187

Booting Android Device on AlarmManager Call

I am building an Android Alarm Applicaion, and used following code for Main Calling Class:

   AlarmManager inst_alarm= (AlarmManager) getSystemService(ALARM_SERVICE); 
   Intent intent =new Intent(MainActivity.this,Alarm.class);
   pintent= PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
   inst_alarm.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pintent);  

  //"cal" is reference of calendar class to get saved time in millisecond.

for Service Class:

public class Alarm extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context c, Intent i) {

    Uri uri_alarm= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

    if(uri_alarm==null)
    {           uri_alarm=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    }
    Ringtone ringtone_alarm=RingtoneManager.getRingtone(c, uri_alarm);
    ringtone_alarm.play();
        }
}

This code work fine but i have few quesions.

Question: 1) Is it possible to alarm bell on scheduled time if android device is switched off(device get power on automatically to ring bell on saved time) as in default application of android device does.

2) Suppose i saved alarm after 8 minutes exactly and i restart my device then alarm start ringing immediately after boot(Because i have used "bootcomplete" for receiver in manifest) but i want to play alarm after 8 minutes exactly not on reboot.(I have saved alarm in shared-preference but how to use it on reboot)

EDIT: Can I use "Power Manager" class or any other class to handle above mentioned situation ?

Upvotes: 2

Views: 591

Answers (2)

oaskamay
oaskamay

Reputation: 274

For (1), if you're talking about complete shutdowns, it's unfortunately impossible. There is no way to do this without specialized hardware (and its corresponding software interface to your app).

For (2), you can do something clever. For example, persistently store the timestamp whenever the device restarts/turns off (you can register a BroadcastReceiver for ACTION_SHUTDOWN in addition to your BOOT_COMPLETE) when you have a pending alarm bell, and use that information on reboot/boot to resume (or stop, depending on the time difference and the alarm bell's "timer") or otherwise re-sync your pending alarm bell logic.

And for your PowerManager question, again if we're talking about complete shutdowns, unfortunately the answer is no.

PowerManager is used to mainly prevent the device from going into deep sleep. Remember, Android devices turn off their CPU's some time after they are locked, preventing your app from doing calculations/processes. This is essentially what the notion of "wakelocks" is about.

Upvotes: 0

Larry Schiefer
Larry Schiefer

Reputation: 15775

1) Is it possible...

No, not from complete power off. You're most likely referring to screen being off and the device in low power state. If the device is turned completely off then alarms cannot wake it. The default Android clock app just wakes it from sleep. If you'd like to wake up the screen and play sound, you'll have to create a Service which can be activated via your WakefulBroadcastReceiver and have it start the appropriate UI. If you do not, then the system can go right back to seep when your onReceive() is done.

2) ...want to play 8 minutes after...

What you're likely seeing is a previous expiration of your Alarm firing and you're waking up the device (not powering it on.) Alarms are not retained across power cycles or reboots. If you wish to have them stick around after a true reboot, then you'd have to manage that yourself. The PowerManager and AlarmManager do not provide such a facility.

Upvotes: 1

Related Questions