如何判断用户是否在服务锁定屏幕上

时间:2011-08-12 18:45:23

标签: android service locking screen

我有一个在后台运行的简单服务,我只是想知道用户何时在锁定屏幕上,这样我知道何时开始一个过程。

2 个答案:

答案 0 :(得分:27)

结帐a similar question asked here。使用KeyguardManager检查设备是否已锁定。

KeyguardManager kgMgr = 
    (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean showing = kgMgr.inKeyguardRestrictedInputMode();

答案 1 :(得分:0)

1)您需要首先在服务中注册BroadcastReceiver,以便在按下电源按钮时进行监听(打开/关闭屏幕):

@Override public void onCreate() {
    super.onCreate();
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    filter.addAction(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_USER_PRESENT);
    registerReceiver(lockScreenReceiver, filter);
}

final BroadcastReceiver lockScreenReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();
        if (Intent.ACTION_SCREEN_OFF.equals(action)) {
            Log.i(TAG, "ACTION_SCREEN_OFF");
            if (LockScreenHelper.isScreenUnlocked(getBaseContext())) {
                // what does this mean?
                Log.i(TAG, "screen is unlocked\n\n");
            } else {
                // this means device screen is off, and is locked
                Log.i(TAG, "screen is locked\n\n");        
            }

        } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
            Log.i(TAG, "ACTION_SCREEN_ON");
            if (LockScreenHelper.isScreenUnlocked(getBaseContext())) {
                // this means device screen is on, and is unlocked
                Log.i(TAG, "screen is unlocked\n\n");
            } else {
                // this means device screen is on, and is locked
                Log.i(TAG, "screen is locked\n\n");                   
            }
        }

        if (Intent.ACTION_USER_PRESENT.equals(action)) {
            Log.i(TAG, "screen user is present - on and unlocked");
        }
};

2)您可以使用此助手类随时确定屏幕状态:

/**
 * Decides what state the lock screen is in.
 * Logic adapted from chromium open source project:
 * https://github.com/crosswalk-project/chromium-crosswalk/blob/master/chrome/android/java/src/org/chromium/chrome/browser/IntentHandler.java
 */

public class LockScreenHelper {
  private static final String TAG = 
  LockScreenHelper.class.getCanonicalName();

  /**
   * Determine if the screen is on and the device is unlocked;
   * i.e. the user will see what is going on in the main activity.
   *
   * @param context Context
   * @return boolean
   */
  public static boolean isScreenUnlocked(Context context) {
    if (!isInteractive(context)) {
        Log.i(TAG, "device is NOT interactive");
        return false;
    } else {
        Log.i(TAG, "device is interactive");
    }

    if (!isDeviceProvisioned(context)) {
        Log.i(TAG, "device is not provisioned");
        return true;
    }

    Object keyguardService = context.getSystemService(Context.KEYGUARD_SERVICE);
    return !((KeyguardManager) keyguardService).inKeyguardRestrictedInputMode();
}

/**
 * @return Whether the screen of the device is interactive (screen may or may not be locked at the time).
 */
@SuppressWarnings("deprecation")
public static boolean isInteractive(Context context) {
    PowerManager manager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        return manager.isInteractive();
    } else {
        return manager.isScreenOn();
    }
}

/**
 * @return Whether the device has been provisioned (0 = false, 1 = true).
 * On a multiuser device with a separate system user, the screen may be locked as soon as this
 * is set to true and further activities cannot be launched on the system user unless they are
 * marked to show over keyguard.
 */
private static boolean isDeviceProvisioned(Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return true;
    }

    if (context == null) {
        return true;
    }

    if (context.getContentResolver() == null) {
        return true;
    }

    return Settings.Global.getInt(context.getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 0) != 0;
}

}