0

I'm trying to create an alarm with a list of time to go off that I already have stored in Parse. With the code I am using now, nothing happens when the time is met. I used to use a service that intents to another activity, but the wakelock doesn't work. So, I'm trying to use the Activity now. But now, nothing happens when the time is met. Can someone help me out? Thank you

I have a Alarm Activity with this code:

@Override
public void onCreate(Bundle savedInstateState) {
    super.onCreate(savedInstateState);

    //wake lock
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, "My Wake Log");
    mWakeLock.acquire();

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    setContentView(R.layout.activity_reminder_screen);

}

The Activity goes off after the Save Button in add an alarm activity is pressed as follow:

//set time into calendar instance
Calendar calendar= Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,mAlarmDetails.timeHour);
calendar.set(Calendar.MINUTE,mAlarmDetails.timeMinute);

AlarmManager reminder = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(this, Alarm.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

reminder.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);

Thank you in advance

stanley santoso
  • 343
  • 1
  • 6
  • 19

1 Answers1

0

Try this code,

PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 1, 
                                              intent, Intent.FLAG_ACTIVITY_NEW_TASK);

/** Getting a reference to the System Service ALARM_SERVICE */
AlarmManager alarmManager = (AlarmManager) getBaseContext()
                           .getSystemService(ALARM_SERVICE);
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day, hour, minute);

/**
 * Converting the date and time in to milliseconds elapsed since
 * epoch
 */
long alarm_time = calendar.getTimeInMillis();
/** Setting an alarm, which invokes the operation at alart_time */
alarmManager.set(AlarmManager.RTC_WAKEUP, alarm_time, operation);
TDG
  • 5,909
  • 3
  • 30
  • 51
userDroid
  • 198
  • 9
  • What is this Gregorian Calendar? I have no information of the year and month. I'm making a daily alarm. – stanley santoso Jul 16 '15 at 04:55
  • I did that and I get log cat error "java.lang.IllegalArgumentException: Must specify a valid wake lock level." – stanley santoso Jul 16 '15 at 05:34
  • 1
    I think this link will help you http://wptrafficanalyzer.in/blog/setting-up-alarm-using-alarmmanager-and-waking-up-screen-and-unlocking-keypad-on-alarm-goes-off-in-android/ – userDroid Jul 17 '15 at 10:35