[英]Help with PROXIMITY_SCREEN_OFF_WAKE_LOCK in Android
I've enabled the proximity wakelock in my app, and it turns off the screen when the proximity sensor detects something.我在我的应用程序中启用了接近唤醒锁,当接近传感器检测到某些东西时它会关闭屏幕。 But there is a problem when the screen wakes back up -- it goes to the lockscreen, not my app.
但是当屏幕重新唤醒时出现了一个问题——它进入了锁屏,而不是我的应用程序。 This happens regardless of the time that the screen was off (even if the sensor is cleared after a few seconds).
无论屏幕何时关闭(即使传感器在几秒钟后被清除),都会发生这种情况。 Here's the code I used:
这是我使用的代码:
int PROXIMITY_SCREEN_OFF_WAKE_LOCK = 32;
mProximityWakeLock = pm.newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, LOG_TAG);
if(!mProximityWakeLock.isHeld()){
mProximityWakeLock.acquire();
}
Is there any way to correct that behavior?有没有办法纠正这种行为?
You can dismiss the lock screen, if it is not a secure one, using:您可以关闭锁定屏幕,如果它不是一个安全的,使用:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
You can either call it on creation to prevent the lock screen from ever appearing or when you need to.您可以在创建时调用它以防止锁定屏幕出现或在需要时调用。 I use it in combination with:
我结合使用它:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Which does not seem to interfere with the proximity lock.这似乎不会干扰接近锁。
I use this code我使用此代码
import android.content.Context;
import android.os.PowerManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private PowerManager mPowerManager;
private PowerManager.WakeLock mWakeLock;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
}
public void activateSensor(View v) {
Toast.makeText(MainActivity.this, "Proximity On", Toast.LENGTH_LONG).show();
if (mWakeLock == null) {
mWakeLock = mPowerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "incall");
}
if (!mWakeLock.isHeld()) {
Log.d(TAG, "New call active : acquiring incall (CPU only) wake lock");
mWakeLock.acquire();
} else {
Log.d(TAG, "New call active while incall (CPU only) wake lock already active");
}
}
public void deactivateSensor(View v) {
Toast.makeText(MainActivity.this, "Proximity Off", Toast.LENGTH_LONG).show();
if (mWakeLock != null && mWakeLock.isHeld()) {
mWakeLock.release();
Log.d(TAG, "Last call ended: releasing incall (CPU only) wake lock");
} else {
Log.d(TAG, "Last call ended: no incall (CPU only) wake lock were held");
}
}
}
In the manifest:在清单中:
<uses-permission android:name="android.permission.WAKE_LOCK" />
In my layout:在我的布局中:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.proximitysensor.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/activity_main_turn_on_screen"
android:onClick="activateSensor"
android:text="@string/activity_main_activate_sensor" />
<Button
android:id="@+id/activity_main_turn_on_screen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:onClick="deactivateSensor"
android:text="@string/activity_main_deactivate_sensor" />
</RelativeLayout>
If you're using mProximityWakeLock.release();
如果您使用的是
mProximityWakeLock.release();
, try using mProximityWakeLock.release(1);
, 尝试使用
mProximityWakeLock.release(1);
The API reference is also hidden, but you can look at the source code: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/os/PowerManager.java#PowerManager.WakeLock.release%28int%29 API的引用也是隐藏的,不过可以看源码: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/os /PowerManager.java#PowerManager.WakeLock.release%28int%29
Besides wakeLock from PowerManager you must create KeyGuard lock like this:除了 PowerManager 中的 wakeLock,您还必须像这样创建 KeyGuard 锁:
private WakeLock mWakeLock = null;
private KeyguardLock mKeyguardLock = null;
public void enableProximitySensor() {
PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, LOG_TAG);
mWakeLock.acquire();
KeyguardManager km = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
mKeyguardLock = mManager.newKeyguardLock( KEYGUARD_TAG );
}
public void disableProximitySensor() {
if ( null != mWakeLock ) {
mWakeLock.release();
mWakeLock = null;
}
if ( null != mKeyguardLock ) {
mKeyguardLock.disableKeyguard();
mKeyguardLock = null;
}
}
Create this lock only for the same time as you acquire proximity wake lock and lock screen will work all other time in your application.仅在您获得接近唤醒锁定的同时创建此锁定,并且锁定屏幕将在您的应用程序中的所有其他时间工作。
private WakeLock mProximityWakeLock= null;
mProximityWakeLock= pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP , LOG_TAG);
Acquiring lock when screen needed to turn on and releasing when needed to off ie reverse to above wakeLock will work fine.在需要打开屏幕时获取锁定并在需要关闭时释放,即反转到上面的wakeLock 将正常工作。
How did u manage to turn back the screen on?你是如何设法重新打开屏幕的? I managed to turn the screen off, when the sensor is covered, using:
当传感器被覆盖时,我设法关闭屏幕,使用:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
etWindow().setAttributes(lp);
and after this, "releasing" or "reseting" the cover from/on the sensor, won't execute the other part of the code:在此之后,从/在传感器上“释放”或“重置”盖板,将不会执行代码的其他部分:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
etWindow().setAttributes(lp);
If it is appropriate for your app you can use如果它适合您的应用程序,您可以使用
getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED | LayoutParams.FLAG_IGNORE_CHEEK_PRESSES);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.