问题描述
几年前,我写了一个报警的应用程序在Android 2的工作,而我现在想升级到Android 4。工作具体来说,在三星Galaxy S4。
A few years ago, I wrote an alarm app that worked on Android 2, and I'm now trying to upgrade it to work on Android 4. Specifically, on the Samsung Galaxy S4.
在Android的2,如果手机正在睡觉,它会唤醒手机并显示贪睡或者解聘屏幕在锁屏。
On Android 2, if the phone was sleeping, it would wake the phone up and display a "Snooze or Dismiss" screen over the lock screen.
在安卓4.0,它唤醒了电话了,但你要解锁,然后打开通知区域,然后单击警报的通知,之前你可以打辞退。
On Android 4, it wakes the phone up, but you have to unlock it, then open the notifications area, then click the alarm's notification, before you can hit "Dismiss."
我一直用这个code做清醒:
I have always been using this code to do the waking:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
我已阅读在这个问题上8种不同的计算器的问题。他们大多得到code以上,这在Android中2年前为我工作,但在Android的4不工作,但他们都没有帮我解决这个问题。下面是我读,并试图问题:
I have read 8 different stackoverflow questions on this matter. Most of them give the code above, which worked for me years ago in Android 2 but doesn't work in Android 4. But none of them have helped me solve this problem. Here are the questions that I read and tried:
如何启动一个对话框(如报警dimiss /贪睡),其可以在不解锁屏幕
在默认锁屏 Android的活性
Android activity over default lock screen
在锁定屏幕 Android的对话框
Android dialog over lock screen
有没有人有什么在安卓4.0更改的任何想法,可能会造成的?
Does anyone have any ideas about what's changed in Android 4 that may have caused this?
编辑:最小化这是我见过的报警对话框不上来的最简单的例子之一它没有,因为写的,出现在锁屏,但您可以修复与 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
Here is one of the simplest examples I've seen of an alarm dialog that doesn't come up "minimized." It does not, as written, appear over the lockscreen, but you can fix that with WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
http://wptrafficanalyzer.in/blog/setting-up-alarm-using-alarmmanager-and-waking-up-screen-and-unlocking-keypad-on-alarm-goes-off-in-android/
这是写一个FragmentActivity和DialogFragment,但它仍然可以作为一个活动。它使用AlertDialog.Builder使对话框,如果你试图用一个XML布局做到这一点,它不会工作。为什么呢?
It's written with a FragmentActivity and a DialogFragment, but it still works as an Activity. It uses an AlertDialog.Builder to make the dialog, and if you try to do it with an XML layout, it won't work. Why?
推荐答案
我想通了,得到的答复是跟我想象中完全不同。
I figured it out, and the answer was very different from what I expected.
这一块code被列入由机器人2闹钟样品,在AlarmAlert.java活动:
This piece of code was included in the alarm clock sample from Android 2, in the AlarmAlert.java Activity:
@Override
protected void onStop() {
super.onStop();
// Don't hang around.
finish();
}
有关的参考,你可以看到的例子code文件中的Git的过去right这里,包含上述的onStop
功能。它从来没有在Android的2引起了问题。
For reference, you can see the file from the example code in Git's past right here, containing the above onStop
function. It never caused a problem in Android 2.
但在安卓4.0,如果手机被关闭,该的onStop
将开火权前的电话醒了,有效地减少的活动。有一次,我取消了这一功能,它会立即再次工作。
But in Android 4, if the phone was off, this onStop
would fire right before the phone woke up, effectively "minimizing" the Activity. Once I removed this function, it immediately worked again.
但我不知道,这是其他人喜欢@radley和@Guardanis所得到的问题?这似乎不大可能,但请让我知道,如果这解决你的问题了。
But I wonder, is this the problem that other people like @radley and @Guardanis are getting? It seems unlikely, but please let me know if this fixes your problems too.
如果您正在访问的未来这个答案,你得到这个问题,我会尝试是:
If you're visiting this answer in the future, and you're getting this problem, what I would try is:
-
取出任何
的onStop
功能。
将此code的活动:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
请确保您使用的是全屏幕主题,而不是一个对话的主题。
Make sure you're using a full screen theme, and not a dialog theme.
这不有所作为我,但你可以尝试在清单中明确设置showOnLockScreen:<活动机器人:名称=com.example.MyActivity机器人:showOnLockScreen =真/>
This didn't make a difference for me, but you could try setting showOnLockScreen explicitly in the manifest: <activity android:name="com.example.MyActivity" android:showOnLockScreen="true"/>
第二件事并没有有所作为但对我来说,你可以尝试是增加的标志 WindowManager.LayoutParams.FLAG_FULLSCREEN
A second thing that didn't make a difference for me but you might try is adding the flag WindowManager.LayoutParams.FLAG_FULLSCREEN
我希望这可以帮助其他人!
I hope this helps other people!
这篇关于Android的银河S4 - 活动是可见在锁屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!