Android Open Source - androidphotobackup Photo Backup Alarm Receiver






From Project

Back to project page androidphotobackup.

License

The source code is released under:

Apache License

If you think the Android project androidphotobackup 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 com.brightsilence.dev.androidphotobackup;
//from   www . j av  a  2  s  .com
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.preference.PreferenceManager;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.util.Log;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;

/**
 * When the alarm fires, this WakefulBroadcastReceiver receives the broadcast Intent
 * and then starts the IntentService {@code SampleSchedulingService} to do some work.
 */
public class PhotoBackupAlarmReceiver extends WakefulBroadcastReceiver {

    public static final String TAG = "PhotoBackup::PhotoBackupAlarmReceiver";

    // 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) {
        Log.d(TAG,"onReceive - Alarm triggered");
        // 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, PhotoBackupService.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, PhotoBackupAlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        long alarmTime = sharedPreferences.getLong("backup_trigger_time", 0 );

        // Is the alarm in the past?  If so, setting it will trigger an alarm straight away,
        // which we don't want, so push the time out by a day.
        while( alarmTime < System.currentTimeMillis() )
        {
            alarmTime += AlarmManager.INTERVAL_DAY;
        }

        // TODO: Remove me
        // TODO: This is here for testing only - the alarm will trigger immediately as it's set in the past.
        alarmTime = System.currentTimeMillis()-6000;

        Log.d(TAG, "Alarm set for: " + alarmTime+" (now: "+System.currentTimeMillis()+")");

        /*
         * 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,
                                     alarmTime,
                                     AlarmManager.INTERVAL_DAY, alarmIntent);

        // Enable {@code PhotoBackupServiceStarter} to automatically restart the alarm when the
        // device is rebooted.
        ComponentName receiver = new ComponentName(context, PhotoBackupServiceStarter.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.
        alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, PhotoBackupAlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_NO_CREATE);

        if (alarmIntent!= null) {
            alarmMgr.cancel(alarmIntent);
        }

        // Disable {@code PhotoBackupServiceStarter} so that it doesn't automatically restart the
        // alarm when the device is rebooted.
        ComponentName receiver = new ComponentName(context, PhotoBackupServiceStarter.class);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP);
    }
    // END_INCLUDE(cancel_alarm)
}




Java Source Code List

com.brightsilence.dev.androidphotobackup.ApplicationTest.java
com.brightsilence.dev.androidphotobackup.DropBoxWrapper.java
com.brightsilence.dev.androidphotobackup.PhotoBackupAlarmReceiver.java
com.brightsilence.dev.androidphotobackup.PhotoBackupServiceStarter.java
com.brightsilence.dev.androidphotobackup.PhotoBackupService.java
com.brightsilence.dev.androidphotobackup.PhotoBackupSettingsActivity.java
com.brightsilence.dev.androidphotobackup.TimePreference.java
com.brightsilence.dev.androidphotobackup.ZipInputStream.java