即使手机处于锁定模式,也能显示活动情况[英] Activity to be displayed even when phone is in locked mode

本文是小编为大家收集整理的关于即使手机处于锁定模式,也能显示活动情况的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我的问题与这个问题相似 我如何唤醒抬高Android设备并跳过屏幕锁

我想从广播接收器显示一个对话框,但是Android API不允许我这样做,因此我正在使用从那里开始活动并将此活动的主题更改为主题.

现在,即使手机处于锁定模式/睡眠模式,我也希望显示此活动.

屏幕我能够使用以下标志打开BU,但钥匙防护(非安全)我必须手动解锁.我无法在锁定屏幕上看到我的窗户.

区别在于我不使用全屏活动,即

android:theme="@android:style/Theme.Dialog

在我的代码中,我正在使用

Window w = getWindow();
w.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | 
           WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | 
           WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

推荐答案

我无法通过使用这些标志成功实现此功能,但是我确实通过使用 wakelock .以下是我要做的:

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

}

当然,您仍然需要在清单文件中声明许可.

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>

本文地址:https://www.itbaoku.cn/post/102347.html

问题描述

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"/>