I'm trying to create an alarm with a list of time to go off that I already have stored in Parse. I am able to have the alarm goes off so far. But now I want it to go off even when the device is asleep. I understand I need to implement wakelock into my service which I already did. I also understand that I need to include "FLAG_SHOW_WHEN_LOCKED | FLAG_TURN_SCREEN_ON, FLAG_FULLSCREEN". Currrently the only way I know is using get Window. But I know getWindow is not for service. So, I need some help on how to solve this.
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();
//wake lock
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, "My Wake Log");
mWakeLock.acquire();
//This code below doesn't work because getWindow is not for Service. what is my other option?
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);
//start alarm screen
Intent intent = new Intent(this, AlarmScreenActivity.class);
ReminderIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
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);
Thank you in advance
UPDATE I tried turning my service into an Activity and having this code intsead:
public class AlarmService extends Activity {
private MediaPlayer mMediaPlayer;
private PowerManager.WakeLock mWakeLock;
@Override
public void onCreate(Bundle savedInstateState) {
super.onCreate(savedInstateState);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(/***PowerManager.FULL_WAKE_LOCK*/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_alarm_screen);
}
I also changed the getService in the pending intent from the button into getActivity. But after I changed these, nothing show up. the alarm doesnt appear.