Back to project page ClinicalTrialTracker.
The source code is released under:
Apache License
If you think the Android project ClinicalTrialTracker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.yuanwei.android; /* w ww . ja v a 2s. co m*/ import android.app.AlarmManager; import android.app.PendingIntent; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.support.v4.content.WakefulBroadcastReceiver; import java.util.Calendar; import com.redapple.android.R; /** * When the alarm fires, this WakefulBroadcastReceiver receives the broadcast Intent * and then starts the IntentService {@code SampleSchedulingService} to do some work. */ public class AlarmReceiver extends WakefulBroadcastReceiver { // 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. */ Intent service = new Intent(context, RssDownloadingSchedulingService.class); // Start the service, keeping the device awake while it is launching. startWakefulService(context, service); // 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, AlarmReceiver.class); alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); SharedPreferences share = context.getSharedPreferences(Constants.SHARED_PREFERENCES, Context.MODE_PRIVATE); int mode =share.getInt(Constants.UPDATE_MODE, 0); //A fixed timing is not a good idea for server. Changed into fixed time interval. July 11th 2014 @Yuanwei Chen /* * * 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); */ 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,share.getInt(Constants.UPDATE_HOUR, 8)); calendar.set(Calendar.MINUTE, share.getInt(Constants.UPDATE_MINUTE, 30)); switch (mode){ case R.id.fixed_intervals_one_day: alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent); break; case R.id.fixed_intervals_two_day: alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY*2, alarmIntent); break; case R.id.fixed_intervals_one_week: alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY*7, alarmIntent); break; default: break; } // Enable {@code SampleBootReceiver} to automatically restart the alarm when the // device is rebooted. ComponentName receiver = new ComponentName(context, BootReceiver.class); PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); } // 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); } // Disable {@code SampleBootReceiver} so that it doesn't automatically restart the // alarm when the device is rebooted. ComponentName receiver = new ComponentName(context, BootReceiver.class); PackageManager pm = context.getPackageManager(); pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); } // END_INCLUDE(cancel_alarm) }