0

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.

stanley santoso
  • 343
  • 1
  • 6
  • 19

2 Answers2

0

You do not need to hold wake lock. Only do that when you want to prevent device from going to sleep. In your case, you simply want device to wake you up. Using either RTC_WAKEUP or ELAPSED_REALTIME_WAKEUP will accomplish this goal. You can obtain the wake lock upon waking up to prevent device from immediately going back to sleep if you are going to process the event asynchronously.

RTC_WAKEUP is not working

RTC_WAKEUP will not switch on the screen, all it does is wakes up thee cpu so that your job is done. For the Screen to be turned on you need a FULL wakelock to be acquired.

Community
  • 1
  • 1
Phuong Nguyen
  • 909
  • 7
  • 20
  • I want the alarm to go off even when the device is asleep. As you can see, I already have RTC_WAKEUP in my activity when adding the alarm. It doesn't wake the phone up. – stanley santoso Jul 15 '15 at 23:51
  • Have you check to make sure your time calculation is correct? Print out your alarm time and System.currentTimeMillis() and see if the difference is the expected value. – Phuong Nguyen Jul 15 '15 at 23:53
  • The time is right for sure. I know that because it works fine if I remove the whole wakelock. But then it will only goes off after I unlock the screen/phone. – stanley santoso Jul 15 '15 at 23:55
  • Btw, why are you going through a service to launch an activity? You could have the pending intent launch the activity directly. PendingIntent pintent = PendingIntent.getActivity(SomeActivity.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT); – Phuong Nguyen Jul 15 '15 at 23:58
  • So I guess, if I turn this service into an activity, problem solved? I will give that a try soon. – stanley santoso Jul 16 '15 at 00:03
  • I put some update in the question. I switch to an activity. now, the alarm doesn't do anything. – stanley santoso Jul 16 '15 at 00:45
  • Post logcat of the alarm triggering – Phuong Nguyen Jul 16 '15 at 04:22
0

You need either a wakeful intent service or a WakefulBroadcastReceiver - change the pending intent to a broadcast (PendingIntent pendingIntent = PendingIntent.getBroadcast...) set up a receiver and in the receiver launch a Wakeful Intent Service - or set up a WakefulBroadcastReceiver. The alarm managerholds a wakelock while onReceive runs - by using a WIS (or a WBR, haven't use those) you essentially acquire a wakelock that is active between onReceive ends and your service starts. See: https://stackoverflow.com/a/20332558/281545

Community
  • 1
  • 1
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361