I'm trying to create an alarm with a list of time to go off that I already have stored in Parse.
I have a AlarmService with this code:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Toast.makeText(this,"onStartCommand()",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, AlarmScreenActivity.class);
startActivity(intent);
return flags;
}
The Service 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, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
reminder.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);
When the time is met, the app crashes and my Log says:
java.lang.RuntimeException: Unable to start service com.example......AlarmService@16859914 with Intent { flg=0x4 cmp=com.example.....AlarmService (has extras) }: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
I don't quite understand this log error. Can someone explain and help me solve this issue?
The Toast from the AlarmService also never appeared.
another question, do I have to initiate my service from the button? is there a way to do this without using a button? I think my app will work better if I can do this without the button since the time information is online in parse.
I also want my alarm to go off everyday. I currently have information of hour and minute. Do I need some information to tell it to go off everyday?
Thank you in advance
UPDATE
I added a wake lock so it will wake my phone into my Service as follow:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Toast.makeText(this,"onStartCommand()",Toast.LENGTH_SHORT).show();
//wake lock
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, "My Wake Log");
mWakeLock.acquire();
//start reminder screen
Intent intent = new Intent(this, AlarmScreenActivity.class);
ReminderIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
return flags;
}
Now I have another Log that says:
java.lang.RuntimeException: Unable to start service com.example....AlarmService@129539b6 with Intent { flg=0x4 cmp=com.....AlarmService (has extras) }: java.lang.IllegalArgumentException: Must specify a valid wake lock level.
What does this mean?