Android Open Source - android-002 Alarm Receiver






From Project

Back to project page android-002.

License

The source code is released under:

Apache License

If you think the Android project android-002 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.michaelfitzmaurice.dailyselfie;
/* w  w w . j a v  a2 s .  com*/
import static com.michaelfitzmaurice.dailyselfie.SelfieListActivity.LOG_TAG;

import java.util.Date;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;

public class AlarmReceiver extends BroadcastReceiver {
  
  private static final int NOTIFICATION_ID = 1;

  public AlarmReceiver() {}

  @Override
  public void onReceive(Context context, Intent intent) {
    Log.d(LOG_TAG, "Received a broadcast");
    Log.d(LOG_TAG, "Intent is " + intent);
    Log.d(LOG_TAG, "Context is " + context);
    
    notifyReceived(context);

    Intent notificationIntent = 
      new Intent(context, SelfieListActivity.class);
    PendingIntent pendingIntent = 
      PendingIntent.getActivity(context, 
                    0,
                    notificationIntent, 
                    Intent.FLAG_ACTIVITY_NEW_TASK);
    
    Uri soundUri = 
      RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    long[] vibratePattern = { 0, 200, 200, 300 };

    Notification.Builder notificationBuilder = 
      new Notification.Builder(context)
        .setTicker( context.getString(R.string.notification_ticker) )
        .setSmallIcon(android.R.drawable.ic_menu_camera)
        .setAutoCancel(true).setContentTitle( 
          context.getString(R.string.notification_content_title) )
        .setContentText( 
          context.getString(R.string.notification_content_text) )
        .setContentIntent(pendingIntent)
        .setSound(soundUri).setVibrate(vibratePattern);

    NotificationManager notificationManager = 
      (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify( NOTIFICATION_ID,
                  notificationBuilder.build() );
  }
  
  public void notifyReceived(Context context) {

    SharedPreferences sharedPrefences = 
      context.getSharedPreferences(LOG_TAG, Context.MODE_PRIVATE);
    AlarmTimeInterval interval = Alarms.DEFAULT_ALARM_INTERVAL;
    String intervalString = 
      sharedPrefences.getString(Alarms.ALARM_INTERVAL_PREFERENCES_KEY, 
                    null);
    if (intervalString != null) {
      interval = new AlarmTimeInterval(intervalString);
    }
    setNextAlarmDuePref(sharedPrefences, interval);
    }
  
  private void setNextAlarmDuePref(SharedPreferences sharedPrefences, 
                  AlarmTimeInterval interval) {
    
    long nextAlarmDueMs = 
      System.currentTimeMillis() + interval.toMilliseconds();
    Date nextAlarmDueDate = new Date(nextAlarmDueMs);
    Log.d(LOG_TAG, "Persisting next alarm due time in prefs as " 
        + nextAlarmDueDate);
    sharedPrefences
      .edit()
      .putLong(Alarms.NEXT_ALARM_DUE_PREFERENCES_KEY, 
            nextAlarmDueMs)
      .apply();
  }

}




Java Source Code List

com.michaelfitzmaurice.android.modernart.MainActivity.java
com.michaelfitzmaurice.dailyselfie.AlarmReceiver.java
com.michaelfitzmaurice.dailyselfie.AlarmTimeInterval.java
com.michaelfitzmaurice.dailyselfie.Alarms.java
com.michaelfitzmaurice.dailyselfie.SelfieDetailActivity.java
com.michaelfitzmaurice.dailyselfie.SelfieListActivity.java
com.michaelfitzmaurice.dailyselfie.SelfieListViewAdapter.java
com.michaelfitzmaurice.dailyselfie.SelfieRecord.java
com.michaelfitzmaurice.dailyselfie.settings.ReminderIntervalDialogPreference.java
com.michaelfitzmaurice.dailyselfie.settings.SettingsActivity.java
com.michaelfitzmaurice.dailyselfie.settings.SettingsFragment.java
course.labs.contentproviderlab.MockLocationProvider.java
course.labs.contentproviderlab.PlaceDownloaderTask.java
course.labs.contentproviderlab.PlaceRecord.java
course.labs.contentproviderlab.PlaceViewActivity.java
course.labs.contentproviderlab.PlaceViewAdapter.java
course.labs.contentproviderlab.provider.PlaceBadgeContentProvider.java
course.labs.contentproviderlab.provider.PlaceBadgesContract.java
course.labs.contentproviderlab.provider.PlaceBadgesContract.java
course.labs.graphicslab.BubbleActivity.java
course.labs.locationlab.MockLocationProvider.java
course.labs.locationlab.PlaceDownloaderTask.java
course.labs.locationlab.PlaceRecord.java
course.labs.locationlab.PlaceViewActivity.java
course.labs.locationlab.PlaceViewAdapter.java
course.labs.notificationslab.DownloaderTask.java
course.labs.notificationslab.FeedFragment.java
course.labs.notificationslab.FriendsFragment.java
course.labs.notificationslab.MainActivity.java
course.labs.notificationslab.SelectionListener.java
course.labs.notificationslab.TestFrontEndActivity.java