显示活动已通过锁屏

时间:2014-11-02 12:14:54

标签: android wakelock

我正在构建一个闹钟应用程序。 当闹钟响起时,我会发布带有视频的活动。 如果我的屏幕未锁定,则会弹出活动并播放视频,但是当屏幕锁定时,它仅播放音频并打开屏幕。当我手动解锁屏幕时,活动将被解除。

理想情况下,它会通过锁屏和&告诉我这个活动,所以视频即时弹出。

这是该活动最重要的部分(从服务开始)

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();
    }

}

我的日志:

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!!

更新 我在这里发现了一个关于同样问题的SO问题:Why the onPause method is called immediately after onCreate

我注意到onPause被调用了,所以我会在那里找到答案!

然而,亚伯拉罕·菲利普的答案是这个难题的重要部分!

1 个答案:

答案 0 :(得分:3)

你有没有尝试过:

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

你想要解锁屏幕?

在你的情况下,它是:

@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!!");
        }

    }