I think I know the answer, but will this alarm continue to repeat if the phone is rebooted? If not, should I use a Service instead? I want to use the alarm to send out a Notification every few hours.
// AlarmActivity.java public class AlarmActivity<AlarmController> extends Activity { Toast mToast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Watch for button clicks. // Button button = (Button)findViewById(R.id.one_shot); // button.setOnClickListener(mOneShotListener); Button button = (Button)findViewById(R.id.start_repeating); button.setOnClickListener(mStartRepeatingListener); button = (Button)findViewById(R.id.stop_repeating); button.setOnClickListener(mStopRepeatingListener); } private OnClickListener mStartRepeatingListener = new OnClickListener() { public void onClick(View v) { // When the alarm goes off, we want to broadcast an Intent to our // BroadcastReceiver. Here we make an Intent with an explicit class // name to have our own receiver (which has been published in // AndroidManifest.xml) instantiated and called, and then create an // IntentSender to have the intent executed as a broadcast. // Note that unlike above, this IntentSender is configured to // allow itself to be sent multiple times. Intent intent = new Intent(AlarmActivity.this, RepeatingAlarm.class); PendingIntent sender = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0); // We want the alarm to go off 30 seconds from now. long firstTime = SystemClock.elapsedRealtime(); firstTime += 5*1000; // Schedule the alarm! AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTime, 3*60*60*1000, sender); // Tell the user about what we did. if (mToast != null) { mToast.cancel(); } mToast = Toast.makeText(AlarmActivity.this, "Started: Repeating Alarm (every 5s)", Toast.LENGTH_SHORT); mToast.show(); } }; private OnClickListener mStopRepeatingListener = new OnClickListener() { public void onClick(View v) { // Create the same intent, and thus a matching IntentSender, for // the one that was scheduled. Intent intent = new Intent(AlarmActivity.this, RepeatingAlarm.class); PendingIntent sender = PendingIntent.getBroadcast(AlarmActivity.this, 0, intent, 0); // And cancel the alarm. AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.cancel(sender); // Tell the user about what we did. if (mToast != null) { mToast.cancel(); } mToast = Toast.makeText(AlarmActivity.this, "Stopped: Repeating Alarm", Toast.LENGTH_SHORT); mToast.show(); } }; } // RepeatingAlarm.java public class RepeatingAlarm extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //HACK: http://stackoverflow.com/questions/368094/system-currenttimemillis-vs-new-date-vs-calendar-getinstance-gettime Toast.makeText(context, "Repeating Alarm now!", Toast.LENGTH_SHORT).show(); // Send a notification. } } -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en