更新时间:2023-02-26 22:01:42
我不通过使用这些标志实现这个功能成功,但我没有使用的 WakeLock 和KeyguardLock.下面是我做的:
公共类DismissLock延伸活动{
电源管理器时;
WakeLock WL;
KeyguardManager公里;
KeyguardLock KL;
@覆盖
保护无效的onCreate(包savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
Log.i(信息,的onCreate()的DismissLock);
PM =(电源管理器)getSystemService(Context.POWER_SERVICE);
千米=(KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
KL = km.newKeyguardLock(信息);
WL = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE,INFO);
wl.acquire(); //唤醒屏幕
kl.disableKeyguard(); //关闭该键盘保护
的setContentView(R.layout.main);
}
@覆盖
保护无效的onPause(){
// TODO自动生成方法存根
super.onPause();
wl.release(); //当activiy停顿,我们应该realse的wakelock
}
@覆盖
保护无效onResume(){
// TODO自动生成方法存根
super.onResume();
wl.acquire(); //必须调用这个!
}
}
当然,你仍然需要声明的许可清单文件。
<使用-权限的Android:名称=android.permission.WAKE_LOCK/>
<使用-权限的Android:名称=android.permission.DISABLE_KEYGUARD/>
My Question is similar to this one How can I wake an android device up and skip the screenlock
I want to display a dialogue box from broadcast receiver, but Android API is not allowing me to do that therefore I am using starting an activity from there and changing the theme of this activity to Theme.
Now I want this activity to be displayed even when phone is in locked mode/ sleep mode.
Screen I am able to turn on bu using below flags but Key Guard (Non Secured) I have to unlock manually. I am not able to see my window over locked screen.
The difference is that I am not using a full screen Activity i.e.
android:theme="@android:style/Theme.Dialog
in my code I am using
Window w = getWindow();
w.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
I don't succeed in achieving this feature by using these Flags, but I did succeed by using the WakeLock and KeyguardLock. Below is what I do:
public class DismissLock extends Activity {
PowerManager pm;
WakeLock wl;
KeyguardManager km;
KeyguardLock kl;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.i("INFO", "onCreate() in DismissLock");
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
km=(KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
kl=km.newKeyguardLock("INFO");
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.ON_AFTER_RELEASE, "INFO");
wl.acquire(); //wake up the screen
kl.disableKeyguard();// dismiss the keyguard
setContentView(R.layout.main);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
wl.release(); //when the activiy pauses, we should realse the wakelock
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
wl.acquire();//must call this!
}
}
Of course, you still need to declare the permission in the manifest file.
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>