Android Open Source - InitHub-Android Alarm Receiver






From Project

Back to project page InitHub-Android.

License

The source code is released under:

MIT License

If you think the Android project InitHub-Android 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.solutiosoft.android.inithub;
/*from w  ww .j ava  2s .c o m*/
import java.util.ArrayList;

import com.solutiosoft.android.inithub.dao.InitHubDatabaseManager;
import com.solutiosoft.android.inithub.service.MessageIntentService;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.util.Log;

public class AlarmReceiver extends BroadcastReceiver {
  private static final String TAG = "AlarmReceiver";
  private static final int ALARM_ID = 234324243;
  
  public AlarmReceiver() {
  }

  @Override
  public void onReceive(Context context, Intent intent) {
    
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
        wl.acquire();

        
    // TODO: This method is called when the BroadcastReceiver is receiving
    // an Intent broadcast.
    
    // we need to call the intent service here
    Log.d(TAG, "*********** AlarmReceiver.onReceive");
    InitHubDatabaseManager db = new InitHubDatabaseManager(context);
    ArrayList<Integer> subjects = db.getSubscribedSubjects();
    Intent newIntent = new Intent(context, MessageIntentService.class);
    Bundle bundle = new Bundle();
    bundle.putIntegerArrayList(Constants.MESSAGE_SERVICE_SUBJECTS, subjects);
    bundle.putInt(Constants.MESSAGE_SERVICE_ACTION, Constants.MESSAGE_ACTION_PROCESS_SUBJECTS);
    //bundle.putLong(Constants.SUBJECT_ID, subjectId);
    newIntent.putExtras(bundle);
    context.startService(newIntent);
    
    wl.release();
    
  }
  
  public void startAlarm(Context cxt) {
    
    // TODO: need to make interval configurable
    // TODO: need to restart alarm on reboot
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(cxt);
    String sync_frequency = sharedPrefs.getString(Constants.SYNC_FREQUENCY, null);
    
    Intent intent = new Intent(cxt, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(cxt, ALARM_ID, intent, 0);
    AlarmManager alarmManager = (AlarmManager) cxt.getSystemService(Context.ALARM_SERVICE);
    //alarmManager.cancel(pendingIntent);

    if(sync_frequency!= null){
      if(sync_frequency.equals("-1")){
        Log.d(TAG, "Canceling alarm.........");
        alarmManager.cancel(pendingIntent);
      }
      else {
        int i;
        try {
          i = Integer.parseInt(sync_frequency);
          Log.d(TAG, "Starting alarm... Frequency: " + i + " minutes");
          i = Integer.parseInt(sync_frequency);
          alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 0, (60 * 1000), pendingIntent);
        } catch (NumberFormatException e) {
          Log.d(TAG, "NumberFormatException... This should not happen. Canceling alarm.");
          alarmManager.cancel(pendingIntent);
        }
        
      }
      
    }
    
    
  }
  
  public void cancelAlarm(Context cxt){
    Intent intent = new Intent(cxt, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(cxt, ALARM_ID, intent, 0);
    AlarmManager alarmManager = (AlarmManager) cxt.getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);    
  }
}




Java Source Code List

com.solutiosoft.android.inithub.AlarmReceiver.java
com.solutiosoft.android.inithub.ApiHelper.java
com.solutiosoft.android.inithub.BootupReceiver.java
com.solutiosoft.android.inithub.Constants.java
com.solutiosoft.android.inithub.Utils.java
com.solutiosoft.android.inithub.dao.InitHubDatabaseManager.java
com.solutiosoft.android.inithub.dao.SubjectDatabaseHelper.java
com.solutiosoft.android.inithub.entities.Message.java
com.solutiosoft.android.inithub.entities.Subject.java
com.solutiosoft.android.inithub.entities.User.java
com.solutiosoft.android.inithub.provider.MessageProvider.java
com.solutiosoft.android.inithub.provider.SubjectProvider.java
com.solutiosoft.android.inithub.service.MessageIntentService.java
com.solutiosoft.android.inithub.service.SubjectIntentService.java
com.solutiosoft.android.inithub.ui.InboxActivity.java
com.solutiosoft.android.inithub.ui.LoginActivity.java
com.solutiosoft.android.inithub.ui.MessageActivity.java
com.solutiosoft.android.inithub.ui.MessageAddActivity.java
com.solutiosoft.android.inithub.ui.SettingsActivity.java
com.solutiosoft.android.inithub.ui.SplashActivity.java
com.solutiosoft.android.inithub.ui.SubjectActivity.java