在Android中帮助PROXIMITY_SCREEN_OFF_WAKE_LOCK

|| 我已在我的应用中启用了接近唤醒锁,当接近传感器检测到某些东西时,它将关闭屏幕。但是,当屏幕唤醒时会出现问题-进入锁定屏幕,而不是我的应用程序。无论关闭屏幕的时间如何,这种情况都会发生(即使几秒钟后清除了传感器)。这是我使用的代码:
int PROXIMITY_SCREEN_OFF_WAKE_LOCK = 32;    
mProximityWakeLock = pm.newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, LOG_TAG);
if(!mProximityWakeLock.isHeld()){
    mProximityWakeLock.acquire();
}
有什么办法可以纠正这种行为?     
已邀请:
        您可以使用以下方法关闭锁定屏幕(如果它不是安全的屏幕):
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
您可以在创建时调用它,以防止出现锁定屏幕,或者在需要时调用它。我将其与:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
似乎不会干扰接近锁。     
        我用这个代码
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\");
        }
    }

}
在清单中:
<uses-permission android:name=\"android.permission.WAKE_LOCK\" />
在我的布局中:
<?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>
    
        如果您使用的是
mProximityWakeLock.release();
,请尝试使用
mProximityWakeLock.release(1);
该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     
        除了从PowerManager唤醒唤醒之外,还必须创建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;
  }
}
仅在获取接近唤醒锁的同时创建此锁,并且锁屏将在应用程序中的所有其他时间工作。     
        您如何设法重新打开屏幕?当设法覆盖传感器时,我设法使用以下方法关闭了屏幕:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
etWindow().setAttributes(lp);
然后,从传感器上/“释放”或“重置”盖板,将不会执行代码的其他部分:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
etWindow().setAttributes(lp);
    
        
private WakeLock mProximityWakeLock= null;

mProximityWakeLock= pm.newWakeLock(PowerManager.FULL_WAKE_LOCK|PowerManager.ACQUIRE_CAUSES_WAKEUP , LOG_TAG);
在需要打开屏幕时获取锁定,并在需要关闭屏幕时释放锁定(即返回上述wakeLock)可以正常工作。     
        如果适合您的应用,则可以使用
getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED | LayoutParams.FLAG_IGNORE_CHEEK_PRESSES);
    

要回复问题请先登录注册