- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在构建一个闹钟应用程序。当闹钟响起时,我会启动一个带有视频的 Activity 。如果我的屏幕未锁定,Activity 会弹出并播放视频,但当屏幕锁定时,它只会播放音频并打开屏幕。当我手动解锁屏幕时, Activity 被取消。
理想情况下,它会通过锁屏并向我展示 Activity ,因此会弹出视频即时性。
这是该 Activity 最重要的部分(它从服务开始)
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,所以我会在那里找到我的答案!
尽管如此,亚伯拉罕·菲利普的回答是拼图的重要组成部分!
最佳答案
你试过吗:
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!!");
}
}
关于android - 显示 Activity 通过锁屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26699175/
如何从 Android-Native-Screen 打开 flutter 屏幕? 我开了一个 Android-Native-Screen 从现有我们 flutter 项目 . 所以,在 Android
有人知道如何使用 java 和 google chrome 最大化 selenium webdriver 窗口吗? 我已经尝试过一些命令,例如maximize () window (),但没有成功。
Redmi A65 智能电视今天正式开卖,4K 超高清大屏 + 立体声扬声器,到手价 2599 元。 IT之家了解到,Redmi A65 智能电视搭载了一块 65 英寸 4K 屏,支持 HDR
9 月 26 日消息小米电视大师「至尊纪念版」将在 9 月 29 日发布,根据今天小米集团大家电部总经理李肖爽放出的海报显示,小米电视大师至尊版有两款。 小米电视大师「至尊纪念版」是小米首款
华为手环 6 今日在海外正式发布,英文名 Band 6。这款产品是华为手环 4 的升级版,屏幕尺寸由 0.96 英寸升级为 1.47 英寸 AMOLED 屏,分辨率 194 x 368。该手环外形类
我是一名优秀的程序员,十分优秀!