Android Open Source - CSCI567---Workspace Sample Alarm Receiver






From Project

Back to project page CSCI567---Workspace.

License

The source code is released under:

MIT License

If you think the Android project CSCI567---Workspace listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package csci567.alarmexample;
//from   w  w w .java  2s.c o  m
import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.widget.Toast;




public class SampleAlarmReceiver extends BroadcastReceiver { 
    // The app's AlarmManager, which provides access to the system alarm services.
    private AlarmManager alarmMgr;
    // The pending intent that is triggered when the alarm fires.
    private PendingIntent alarmIntent;
  
    @Override
    public void onReceive(Context context, Intent intent) {   
        // BEGIN_INCLUDE(alarm_onreceive)
        /* 
         * If your receiver intent includes extras that need to be passed along to the
         * service, use setComponent() to indicate that the service should handle the
         * receiver's intent. For example:
         * 
         * ComponentName comp = new ComponentName(context.getPackageName(), 
         *      MyService.class.getName());
         *
         * // This intent passed in this call will include the wake lock extra as well as 
         * // the receiver intent contents.
         * startWakefulService(context, (intent.setComponent(comp)));
         * 
         * In this example, we simply create a new intent to deliver to the service.
         * This intent holds an extra identifying the wake lock.
         */
      PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();
      Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show();
      wl.release();
        // END_INCLUDE(alarm_onreceive)
    }

    // BEGIN_INCLUDE(set_alarm)
    /**
     * Sets a repeating alarm that runs once a day at approximately 8:30 a.m. When the
     * alarm fires, the app broadcasts an Intent to this WakefulBroadcastReceiver.
     * @param context
     */
    public void setAlarm(Context context) {
        alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, SampleAlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        // Set the alarm's trigger time to 8:30 a.m.
        calendar.set(Calendar.HOUR_OF_DAY, 8);
        calendar.set(Calendar.MINUTE, 30);
  
        /* 
         * If you don't have precise time requirements, use an inexact repeating alarm
         * the minimize the drain on the device battery.
         * 
         * The call below specifies the alarm type, the trigger time, the interval at
         * which the alarm is fired, and the alarm's associated PendingIntent.
         * It uses the alarm type RTC_WAKEUP ("Real Time Clock" wake up), which wakes up 
         * the device and triggers the alarm according to the time of the device's clock. 
         * 
         * Alternatively, you can use the alarm type ELAPSED_REALTIME_WAKEUP to trigger 
         * an alarm based on how much time has elapsed since the device was booted. This 
         * is the preferred choice if your alarm is based on elapsed time--for example, if 
         * you simply want your alarm to fire every 60 minutes. You only need to use 
         * RTC_WAKEUP if you want your alarm to fire at a particular date/time. Remember 
         * that clock-based time may not translate well to other locales, and that your 
         * app's behavior could be affected by the user changing the device's time setting.
         * 
         * Here are some examples of ELAPSED_REALTIME_WAKEUP:
         * 
         * // Wake up the device to fire a one-time alarm in one minute.
         * alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
         *         SystemClock.elapsedRealtime() +
         *         60*1000, alarmIntent);
         *        
         * // Wake up the device to fire the alarm in 30 minutes, and every 30 minutes
         * // after that.
         * alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
         *         AlarmManager.INTERVAL_HALF_HOUR, 
         *         AlarmManager.INTERVAL_HALF_HOUR, alarmIntent);
         */
        
        // Set the alarm to fire at approximately 8:30 a.m., according to the device's
        // clock, and to repeat once a day.
        //alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,  
         //       calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
        
       //Fire Repeating Alarm every one minute.
        alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, AlarmManager.INTERVAL_FIFTEEN_MINUTES/15, 
            AlarmManager.INTERVAL_FIFTEEN_MINUTES/15, alarmIntent);
        
               
    }
    // END_INCLUDE(set_alarm)

    /**
     * Cancels the alarm.
     * @param context
     */
    // BEGIN_INCLUDE(cancel_alarm)
    public void cancelAlarm(Context context) {
        // If the alarm has been set, cancel it.
        if (alarmMgr!= null) {
            alarmMgr.cancel(alarmIntent);
        }      
        
    }
    // END_INCLUDE(cancel_alarm)


}




Java Source Code List

com.example.linearlayoutexample.MainActivity.java
com.example.relativelayoutexample.MainActivity.java
csci567.FragmentExample.Fragmentmanageractivity.java
csci567.FragmentExample.MainActivity.java
csci567.FragmentExample.MyFragment.java
csci567.FragmentExample.MyStaticFragment.java
csci567.alarmexample.MainActivity.java
csci567.alarmexample.SampleAlarmReceiver.java
csci567.asynctaskexample.MainActivity.java
csci567.buttonexample.MainActivity.java
csci567.checkboxexample.MainActivity.java
csci567.doodleexample.MainActivity.java
csci567.doodleexample.SampleAlarmReceiver.java
csci567.doodleexample.SampleBootReceiver.java
csci567.doodleexample.SampleSchedulingService.java
csci567.eventreceiver.DataReceiver.java
csci567.eventreceiver.MainActivity.java
csci567.eventreceiver.RebootReceiver.java
csci567.helloworld.MainActivity.java
csci567.simpledbexample.DBHelper.java
csci567.simpledbexample.MainActivity.java
csci567.suggestionapp.MainActivity.java
csci567.writefile.MainActivity.java
org.ndeftools.boilerplate.AndroidNfcActivity.java
org.ndeftools.boilerplate.DefaultNfcBeamWriterActivity.java
org.ndeftools.boilerplate.DefaultNfcReaderActivity.java
org.ndeftools.boilerplate.DefaultNfcTagWriterActivity.java
org.ndeftools.boilerplate.NdefRecordAdapter.java
org.ndeftools.nfcdemo.TagViewer.java
org.ndeftools.nfcdemo.simulator.FakeTagsActivity.java
org.ndeftools.nfcdemo.simulator.MockNdefMessages.java