Java Code Examples for android.os.PowerManager#isScreenOn()

The following examples show how to use android.os.PowerManager#isScreenOn() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JWebSocketClientService.java    From WebSocketClient with Apache License 2.0 6 votes vote down vote up
/**
 * 检查锁屏状态,如果锁屏先点亮屏幕
 *
 * @param content
 */
private void checkLockAndShowNotification(String content) {
    //管理锁屏的一个服务
    KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
    if (km.inKeyguardRestrictedInputMode()) {//锁屏
        //获取电源管理器对象
        PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
        if (!pm.isScreenOn()) {
            @SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP |
                    PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
            wl.acquire();  //点亮屏幕
            wl.release();  //任务结束后释放
        }
        sendNotification(content);
    } else {
        sendNotification(content);
    }
}
 
Example 2
Source File: HelperNotificationAndBadge.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Is the screen of the device on.
 *
 * @param context the context
 * @return true when (at least one) screen is on
 */
public boolean isScreenOn(Context context) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        boolean screenOn = false;
        for (Display display : dm.getDisplays()) {
            if (display.getState() != Display.STATE_OFF) {
                screenOn = true;
            }
        }
        return screenOn;
    } else {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        return pm.isScreenOn();
    }
}
 
Example 3
Source File: AndroidMessenger.java    From actor-platform with GNU Affero General Public License v3.0 6 votes vote down vote up
private boolean isScreenOn() {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        boolean screenOn = false;
        for (Display display : dm.getDisplays()) {
            if (display.getState() != Display.STATE_OFF) {
                screenOn = true;
            }
        }
        return screenOn;
    } else {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        //noinspection deprecation
        return pm.isScreenOn();
    }
}
 
Example 4
Source File: OBSystemsManager.java    From GLEXP-Team-onebillion with Apache License 2.0 6 votes vote down vote up
public boolean isScreenOn(Context context)
{
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH)
    {
        DisplayManager dm = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        boolean screenOn = false;
        for (Display display : dm.getDisplays())
        {
            if (display.getState() != Display.STATE_OFF)
            {
                screenOn = true;
            }
        }
        return screenOn;
    } else
    {
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        //noinspection deprecation
        return pm.isScreenOn();
    }
}
 
Example 5
Source File: DeviceUtils.java    From JobSchedulerCompat with MIT License 5 votes vote down vote up
@SuppressWarnings({"deprecation", "ConstantConditions"})
public static boolean isIdle(Context context) {
    PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return powerManager.isDeviceIdleMode() || !powerManager.isInteractive();
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
        return !powerManager.isInteractive();
    } else {
        return !powerManager.isScreenOn();
    }
}
 
Example 6
Source File: NLService.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
private boolean isScreenOn() {
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
        // API >= 20
        return pm.isInteractive();
    }

    // API <= 19, use deprecated
    //noinspection deprecation
    return pm.isScreenOn();
}
 
Example 7
Source File: JoH.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isScreenOn() {
    final PowerManager pm = (PowerManager) xdrip.getAppContext().getSystemService(Context.POWER_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        return pm.isInteractive();
    } else {
        return pm.isScreenOn();
    }
}
 
Example 8
Source File: MD5_jni.java    From stynico with MIT License 5 votes vote down vote up
/**
 * 
 * @description: 检查屏幕是否亮着并且唤醒屏幕
 * @date: 2016-1-29 下午2:08:25
 * @author: yems
 */
private void checkScreen(Context context)
{
	// TODO Auto-generated method stub
	PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
	if (!pm.isScreenOn())
	{
		wakeUpAndUnlock(context);
	}

}
 
Example 9
Source File: KeepLiveActivity.java    From Android with MIT License 5 votes vote down vote up
private void checkScreenOn() {
    PowerManager manager = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
    boolean isScreenOn = manager.isScreenOn();
    if (isScreenOn) {
        finish();
    }
}
 
Example 10
Source File: ApiCompatibilityUtils.java    From cronet with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @return Whether the screen of the device is interactive.
 */
@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();
    }
}
 
Example 11
Source File: StarterService.java    From always-on-amoled with GNU General Public License v3.0 5 votes vote down vote up
boolean isScreenOn() {
    if (Utils.isAndroidNewerThanL()) {
        DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
        for (Display display : dm.getDisplays()) {
            if (display.getState() != Display.STATE_OFF) {
                return true;
            }
        }
        return false;
    } else {
        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        return powerManager.isScreenOn();
    }
}
 
Example 12
Source File: DeviceUtils.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
/**
 * 判定屏幕是否点亮
 *
 * @param context
 * @return
 */
public static boolean isScreenOn(Context context) {
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    boolean screen = pm.isScreenOn();

    return screen;
}
 
Example 13
Source File: DevicePolicyManagerUtils.java    From FreezeYou with Apache License 2.0 5 votes vote down vote up
/**
 * 优先 ROOT 模式锁屏,失败则尝试 免ROOT 模式锁屏
 *
 * @param context Context
 */
public static void doLockScreen(Context context) {
    //先走ROOT,有权限的话就可以不影响SmartLock之类的了
    try {
        Process process = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
        outputStream.writeBytes("input keyevent KEYCODE_POWER" + "\n");
        outputStream.writeBytes("exit\n");
        outputStream.flush();
        process.waitFor();
        ProcessUtils.destroyProcess(outputStream, process);
    } catch (Exception e) {
        e.printStackTrace();
    }

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if (pm == null || pm.isScreenOn()) {
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        ComponentName componentName = new ComponentName(context, DeviceAdminReceiver.class);
        if (devicePolicyManager != null) {
            if (devicePolicyManager.isAdminActive(componentName)) {
                devicePolicyManager.lockNow();
            } else {
                openDevicePolicyManager(context);
            }
        } else {
            showToast(context, R.string.devicePolicyManagerNotFound);
        }
    }
}
 
Example 14
Source File: Requirements.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private boolean checkIdleRequirement(Context context) {
  if (!isIdleRequired()) {
    return true;
  }
  PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
  return Util.SDK_INT >= 23
      ? !powerManager.isDeviceIdleMode()
      : Util.SDK_INT >= 20 ? !powerManager.isInteractive() : !powerManager.isScreenOn();
}
 
Example 15
Source File: Util.java    From tracker-control-android with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isInteractive(Context context) {
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT_WATCH)
        return (pm != null && pm.isScreenOn());
    else
        return (pm != null && pm.isInteractive());
}
 
Example 16
Source File: ScreenEventWire.java    From tinybus with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
protected void onStart() {
	super.onStart();
	context.registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON));
	context.registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
	
	// send first event immediately
	PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
	mLastScreenEvent = new ScreenEvent(pm.isScreenOn());
	bus.post(mLastScreenEvent);
	
	bus.register(this);
}
 
Example 17
Source File: Requirements.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
private boolean isDeviceIdle(Context context) {
  PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
  return Util.SDK_INT >= 23
      ? powerManager.isDeviceIdleMode()
      : Util.SDK_INT >= 20 ? !powerManager.isInteractive() : !powerManager.isScreenOn();
}
 
Example 18
Source File: NotificationService.java    From an2linuxclient with GNU General Public License v3.0 4 votes vote down vote up
private boolean filter(StatusBarNotification sbn) {
    String packageName = sbn.getPackageName();
    if (!globalEnabled() || !appEnabled(packageName)) {
        return false;
    }
    boolean usingCustomSettings = isUsingCustomSettings(packageName);
    SharedPreferences sp;
    if (usingCustomSettings) {
        sp = getSharedPreferences(getString(R.string.notification_settings_custom), MODE_PRIVATE);
    } else {
        sp = getSharedPreferences(getString(R.string.notification_settings_global), MODE_PRIVATE);
    }

    boolean isAn2linuxTestNotification = packageName.startsWith("kiwi.root.an2linuxclient");
    if (dontSendIfScreenIsOn(sp, packageName, usingCustomSettings) && !isAn2linuxTestNotification) {
        boolean screenIsOn = false;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            DisplayManager dm = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
            for (Display display : dm.getDisplays()) {
                if (display.getState() == Display.STATE_ON) {
                    // private as in samsung always-on feature, not sure if this is how it works
                    // https://stackoverflow.com/questions/2474367/how-can-i-tell-if-the-screen-is-on-in-android#comment71534994_17348755
                    boolean displayIsPrivate = (display.getFlags() & Display.FLAG_PRIVATE) == Display.FLAG_PRIVATE;
                    if (!displayIsPrivate) {
                        screenIsOn = true;
                        break;
                    }
                }
            }
        } else {
            PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
            if (powerManager.isScreenOn()){
                screenIsOn = true;
            }
        }

        if (screenIsOn) {
            return false;
        }
    }

    int flags = sbn.getNotification().flags;
    if (isOngoing(flags) && blockOngoing(sp, packageName, usingCustomSettings)){
        return false;
    }
    if (isForeground(flags) && blockForeground(sp, packageName, usingCustomSettings)){
        return false;
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH){
        if (isGroupSummary(flags) && blockGroupSummary(sp, packageName, usingCustomSettings)){
            return false;
        }
        if (isLocalOnly(flags) && blockLocalOnly(sp, packageName, usingCustomSettings)){
            return false;
        }
    }
    return priorityAllowed(sp, packageName, usingCustomSettings, sbn.getNotification().priority);
}
 
Example 19
Source File: TDevice.java    From android-project-wo2b with Apache License 2.0 4 votes vote down vote up
public static boolean isScreenOn(Context context)
{
	PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
	return pm.isScreenOn();
}
 
Example 20
Source File: SDKUtils.java    From analyzer-of-android-for-Apache-Weex with Apache License 2.0 4 votes vote down vote up
public static boolean isInteractive(@NonNull Context context) {
    PowerManager manager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    return manager.isScreenOn();

}