怎样在安卓中实现在锁屏状态下弹出对话框,并可以震动和铃声,就像闹钟似的?
====================问题描述====================
我想要在应用弹出对话框,程序在后台运行,当达到条件后弹出对话框并有震动和铃声,但是在锁屏状态下却没反应,有什么办法解决吗?
====================解决方案1====================
// 解锁 KeyguardManager manager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); if( manager.inKeyguardRestrictedInputMode() ){ //处于锁定界面,界面则通过KeyguardLock类方法来解锁 KeyguardLock keyguard = manager.newKeyguardLock(getLocalClassName()); keyguard.disableKeyguard(); }
解锁需要权限:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
//点亮屏幕 PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE); mWakelock = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.SCREEN_DIM_WAKE_LOCK, "SimpleTimer"); mWakelock.acquire(); //....... mWakelock.release();
点亮屏幕需要权限:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
====================解决方案2====================
按照LS的方法 请求强制打开锁,会出现按Power键,系统也不会进入Sleep的现象
建议参考如下方法,让对话框在锁屏界面上方显示
onCreate方法时使用
requestWindowFeature(Window.FEATURE_NO_TITLE); //hide title
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
winParams.flags |= (WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setRequestedOrientation(0);
在OnResume时使用
protected void onResume() {
super.onResume();
acquireWakeLock();
}
private void acquireWakeLock() {
if (mWakelock == null) {
Log.i(TAG, "Activity begin start ");
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakelock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, this.getClass().getCanonicalName());
mWakelock.acquire();
}
在onPause
protected void onPause() {
super.onPause();
......
releaseWakeLock();
}
private void releaseWakeLock() {
if (mWakelock != null && mWakelock.isHeld()) {
mWakelock.release();
mWakelock = null;
}
希望对LZ有所帮助!
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。