26722

Launch activity at certain time

Question:

I'm new to Android development, so I might be missing something obvious. I want to launch an Activity when the user's phone clock hits a specified time (similar to an alarm). However, I'm not sure how I would go about doing this as constant polling of the clock seems inefficient and a waste of resources. Do I need to capture broadcast events from the clock, or use PendingIntents? If someone could point out some <strong>SDK methods/services</strong> I should read about, it would be much appreciated. Thanks.

Answer1:

Take a look at the docs for <a href="http://developer.android.com/reference/android/app/AlarmManager.html" rel="nofollow">android.app.AlarmManager</a>.

This class allows your application to schedule PendingIntents for broadcast at specific times, which sounds like exactly what you're looking for. Just schedule a PendingIntent that launches the desired application.

Be aware that when your alarm fires, the phone will be prevented from sleeping until (and only until) onReceive() finishes executing. If you need to keep the phone awake longer, you may need to implement your own wake lock.

Recommend

  • grails unit test + Thread
  • Android - Are there query-able services that already poll for GPS location?
  • Fortran function returning unallocated array causes segmentation fault
  • How to correctly configure interrupt for INT line of Atmel MXT641T touch chip in Android 5 kernel de
  • How to use threads in Perl?
  • Receive a notification (via BroadcastReceiver) when GPS (or location services) is started/stopped
  • libusb and poll / select
  • Porting a multi-threaded compute intensive job to spark
  • socket.io with node.js not working as expected
  • angular translate update translation table
  • PHPUnit picking up on syslog messages?
  • How to wait for a thread to finish execution in C#?
  • Escape double quotes within double quotes
  • Wait for angular to finish updating the DOM
  • How to capture or listen to browser notifications?
  • iPhone: 5 seconds video capture
  • Printing out Japanese (Chinese) characters
  • How to write string.Contains(someText) in expression Tree
  • What do I do with this error when I run tests in rails?
  • What is the equivalent of Android permissions in iOS development? [duplicate]
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • NUnit 3.0 TestCase const custom object arguments
  • Bash if statement with multiple conditions
  • Does Mobilefirst provide a provision to access web services directly?
  • Android application: how to use the camera and grab the image bytes?
  • JBoss External Properties Files in Classpath
  • How to revert to previous XCode version?
  • How to get Eclipse Oxygen to run on Java 9
  • Hardware Accelerated Image Scaling in windows using C++
  • Time complexity of a program which involves multiple variables
  • Refering to the class itself from within a class mehod in Objective C
  • How to do unit test for HttpContext.Current.Server.MapPath
  • Ajax jQuery multiple calls at the same time - long wait for answer and not able to cancel
  • Azure Cloud Service Web Role web pages do not load
  • swift auto completion not working in Xcode6-Beta
  • Get object from AWS S3 as a stream
  • Finding past revisions of files in StarTeam w/ .NET SDK / C#
  • align graphs with different xlab
  • Cannot Parse HTML Data Using Android / JSOUP
  • C# - Getting references of reference