2

I'm building an alarm clock app. When the alarm goes off, I launch an activity with a video. If my screen is not locked, the activity pops up and plays the video, but when the screen is locked, it only plays the audio and turns the screen on. When I manually unlock the screen, the activity gets dismissed.

Ideally it would get passed the lockscreen & show me the activity, so the video immeditality pops up.

This is the most important part of that activity (it starts from a service)

public class AlarmScreen extends Activity {

    private PowerManager.WakeLock mWakeLock;
    private AudioManager audio;

    private static final int WAKELOCK_TIMEOUT = 60 * 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarm_screen);


        btnDismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //play video
        //getWindow().setFormat(PixelFormat.TRANSLUCENT);
        //if you want the controls to appear
        // videoHolder.setMediaController(new MediaController(this));


        Uri video = Uri.parse("android.resource://" + getPackageName() + "/"
                + R.raw.video1small);
        videoViewAlarm.setVideoURI(video);
        videoViewAlarm.start();

        //Ensure wakelock release
        Runnable releaseWakelock = new Runnable() {

            @Override
            public void run() {
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
                getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

                if (mWakeLock != null && mWakeLock.isHeld()) {
                    mWakeLock.release();
                }
            }
        };

        new Handler().postDelayed(releaseWakelock, WAKELOCK_TIMEOUT);
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onResume() {
        super.onResume();

        // Set the window to keep screen on
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

        // Acquire wakelock
        PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
        if (mWakeLock == null) {
            mWakeLock = pm.newWakeLock((PowerManager.FULL_WAKE_LOCK | PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), TAG);
        }

        if (!mWakeLock.isHeld()) {
            mWakeLock.acquire();
            Log.i(TAG, "Wakelock aquired!!");
        }

    }

    @Override
    protected void onPause() {
        super.onPause();

        if (mWakeLock != null && mWakeLock.isHeld()) {
            mWakeLock.release();
        }
        finish();
    }

}

My Logs:

11-02 16:27:00.430    9977-9977/be.thomascbeerten.yomommaoclock D/AlarmScreen﹕ onCreate
11-02 16:27:00.450    9977-9977/be.thomascbeerten.yomommaoclock D/AlarmScreen﹕ Starting video
11-02 16:27:00.460    9977-9977/be.thomascbeerten.yomommaoclock D/AlarmScreen﹕ onResume
11-02 16:27:00.460    9977-9977/be.thomascbeerten.yomommaoclock I/AlarmScreen﹕ Wakelock aquired!!
11-02 16:27:00.470    9977-9977/be.thomascbeerten.yomommaoclock D/AlarmScreen﹕ OnPause
11-02 16:27:00.940    9977-9977/be.thomascbeerten.yomommaoclock I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@42c1e128 time:28154499
11-02 16:27:01.000    9977-9977/be.thomascbeerten.yomommaoclock D/MediaPlayer﹕ getMetadata
11-02 16:27:13.670    9977-9989/be.thomascbeerten.yomommaoclock W/MediaPlayer﹕ info/warning (3, 0)
11-02 16:27:13.690    9977-9977/be.thomascbeerten.yomommaoclock D/AlarmScreen﹕ onResume
11-02 16:27:13.690    9977-9977/be.thomascbeerten.yomommaoclock I/AlarmScreen﹕ Wakelock aquired!!

Update I found a SO Question regarding the same thing here: Why the onPause method is called immediately after onCreate

I noticed that onPause was called, so I will find my answer there!

Nevertheless, the answer from Abraham Philip was an important piece of the puzzle!

Community
  • 1
  • 1
TomCB
  • 3,983
  • 9
  • 40
  • 66

1 Answers1

3

Have you tried:

KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); 
final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock"); 
kl.disableKeyguard(); 

where you'd like to unlock the screen?

In your case it'd be:

@SuppressWarnings("deprecation")
    @Override
    protected void onResume() {
        super.onResume();

        // Set the window to keep screen on
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

        //New code
        KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE); 
        final KeyguardManager.KeyguardLock kl = km .newKeyguardLock("MyKeyguardLock"); 
        kl.disableKeyguard();

        // Acquire wakelock
        PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
        if (mWakeLock == null) {
            mWakeLock = pm.newWakeLock((PowerManager.FULL_WAKE_LOCK | PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), TAG);
        }

        if (!mWakeLock.isHeld()) {
            mWakeLock.acquire();
            Log.i(TAG, "Wakelock aquired!!");
        }

    }
Abraham Philip
  • 648
  • 9
  • 18
  • 1
    Thanks for your help. It goes past the keyguard, but the activity is immediately dismissed. I see it's starting, but it immediately closes. – TomCB Nov 02 '14 at 14:52
  • 1
    Since you've already mentioned that onPause is getting called, I suppose it's the finish() there that's killing your activity. Were you able to find your answer in that other thread? Otherwise you could just remove the finish() and call it conditionally somewhere else? Glad I could be of help. – Abraham Philip Nov 02 '14 at 17:05
  • 2
    The problem looks bigger then I thought, it looks a bit random sometimes. Sometimes the keyguard gets suspended like it should, so onPause & onResume are called twice each. Sometimes (most of the time) after wakelock is aquired it goes to onPause and stays there.. Any thoughts on this? – TomCB Nov 02 '14 at 17:18
  • 1
    More info: if I check again, the first time onResume gets called twice. If I then put the alarm 1 minute later, after the onPause, onResume does not get called anymore. Really curious, why would it work when I leave it for some time... Driving me nuts ;-) – TomCB Nov 02 '14 at 17:44