下面列出了android.app.KeyguardManager#inKeyguardRestrictedInputMode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean locked = km.inKeyguardRestrictedInputMode();
if (locked) {
IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT);
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())) {
mServiceBoundContext.attachService();
}
}
};
registerReceiver(receiver, filter);
} else {
mServiceBoundContext.attachService();
}
finish();
}
private void requestPermissionsIfNotGranted(String[] perms, int resultCode) {
ArrayList<String> permissionsToAskFor = new ArrayList<>();
if (perms != null) { // This is created (or not) by the child activity
for (String permissionToHave : perms) {
if (!checkPermission(permissionToHave)) {
permissionsToAskFor.add(permissionToHave);
}
}
}
if (permissionsToAskFor.size() > 0) {
for (String permission : permissionsToAskFor) {
Log.i("[Permission] Requesting " + permission + " permission");
}
String[] permissions = new String[permissionsToAskFor.size()];
permissions = permissionsToAskFor.toArray(permissions);
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean locked = km.inKeyguardRestrictedInputMode();
if (!locked) {
// This is to workaround an infinite loop of pause/start in Activity issue
// if incoming call ends while screen if off and locked
ActivityCompat.requestPermissions(this, permissions, resultCode);
}
}
}
private boolean isLocked() {
if (preferences.getBoolean("off_as_locked", false)) {
initPowerManager();
if (!powerManager.isScreenOn()) {
isLocked = true;
return isLocked;
}
}
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
final boolean isKeyguardLocked;
if (Build.VERSION.SDK_INT >= 16)
isKeyguardLocked = keyguardManager.isKeyguardLocked();
else isKeyguardLocked = keyguardManager.inKeyguardRestrictedInputMode();
Mlog.v(logTag, isKeyguardLocked + " " + LOCKSCREEN_APPS.contains(currentPackage));
isLocked = isKeyguardLocked || (currentPackage != null && LOCKSCREEN_APPS.contains(currentPackage));
return isLocked;
}
private void handleIntent(Intent intent) {
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
if (km.inKeyguardRestrictedInputMode() || !ApplicationLoader.isScreenOn) {
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_DIM_BEHIND |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else {
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
if (currentMessageObject == null) {
currentMessageNum = 0;
}
getNewMessage();
}
@Override
public void onResume() {
super.onResume();
if (!mCreated)
return;
if (isInitialized()) {
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
if (!keyguardManager.inKeyguardRestrictedInputMode()) {
startPlayer();
}
} else {
if (mCloseComplete) {
reOpen();
}
}
}
public void requestPermissionIfNotGranted(String permission) {
if (!checkPermission(permission)) {
Log.i("[Permission] Requesting " + permission + " permission");
String[] permissions = new String[] {permission};
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean locked = km.inKeyguardRestrictedInputMode();
if (!locked) {
// This is to workaround an infinite loop of pause/start in Activity issue
// if incoming call ends while screen if off and locked
ActivityCompat.requestPermissions(this, permissions, FRAGMENT_SPECIFIC_PERMISSION);
}
}
}
private void handleIntent(Intent intent) {
isReply = intent != null && intent.getBooleanExtra("force", false);
popupMessages.clear();
if (isReply) {
int account = intent != null ? intent.getIntExtra("currentAccount", UserConfig.selectedAccount) : UserConfig.selectedAccount;
popupMessages.addAll(NotificationsController.getInstance(account).popupReplyMessages);
} else {
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
if (UserConfig.getInstance(a).isClientActivated()) {
popupMessages.addAll(NotificationsController.getInstance(a).popupMessages);
}
}
}
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
if (km.inKeyguardRestrictedInputMode() || !ApplicationLoader.isScreenOn) {
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_DIM_BEHIND |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else {
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
if (currentMessageObject == null) {
currentMessageNum = 0;
}
getNewMessage();
}
/**
* 判断是否锁屏
* @return {@code true} yes, {@code false} no
*/
public static boolean isScreenLock() {
try {
KeyguardManager keyguardManager = AppUtils.getKeyguardManager();
return keyguardManager != null && keyguardManager.inKeyguardRestrictedInputMode();
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "isScreenLock");
}
return false;
}
/**
* Return whether screen is locked.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isScreenLock(@NonNull final Context context) {
KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (km == null) {
return false;
}
return km.inKeyguardRestrictedInputMode();
}
private void handleIntent(Intent intent) {
isReply = intent != null && intent.getBooleanExtra("force", false);
popupMessages.clear();
if (isReply) {
int account = intent != null ? intent.getIntExtra("currentAccount", UserConfig.selectedAccount) : UserConfig.selectedAccount;
popupMessages.addAll(NotificationsController.getInstance(account).popupReplyMessages);
} else {
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
if (UserConfig.getInstance(a).isClientActivated()) {
popupMessages.addAll(NotificationsController.getInstance(a).popupMessages);
}
}
}
KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
if (km.inKeyguardRestrictedInputMode() || !ApplicationLoader.isScreenOn) {
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_DIM_BEHIND |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
} else {
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
if (currentMessageObject == null) {
currentMessageNum = 0;
}
getNewMessage();
}
/**
* Return whether screen is locked.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isScreenLock() {
KeyguardManager km =
(KeyguardManager) Utils.getApp().getSystemService(Context.KEYGUARD_SERVICE);
if (km == null) return false;
return km.inKeyguardRestrictedInputMode();
}
/**
* Shows the selector dialog that allows the user to decide which music
* player should receiver the media button press intent.
*
* @param context
* The context.
* @param intent
* The intent to forward.
* @param keyEvent
* The key event
*/
private void showSelector(Context context, Intent intent, KeyEvent keyEvent) {
KeyguardManager manager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
boolean locked = manager.inKeyguardRestrictedInputMode();
Intent showForwardView = new Intent(Constants.INTENT_ACTION_VIEW_MEDIA_BUTTON_LIST);
showForwardView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
showForwardView.putExtras(intent);
showForwardView.setClassName(context,
locked ? ReceiverSelectorLocked.class.getName() : ReceiverSelector.class.getName());
/* COMMENTED OUT FOR MARKET RELEASE Log.i(TAG, "Media Button Receiver: starting selector activity for keyevent: " + keyEvent); */
if (locked) {
// XXX See if this actually makes a difference, might
// not be needed if we move more things to onCreate?
PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
// acquire temp wake lock
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG);
wakeLock.setReferenceCounted(false);
// Our app better display within 3 seconds or we have
// bigger issues.
wakeLock.acquire(3000);
}
context.startActivity(showForwardView);
}
@Override
protected void onCreate(Bundle state) {
super.onCreate(state);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean isOn = Build.VERSION.SDK_INT < 7 || pm.isScreenOn();
boolean isKG = km.inKeyguardRestrictedInputMode();
if (!isOn || isKG) {
finish();
return;
}
TextView tv = new TextView(this);
tv.setText("Protifying code...");
float d = getResources().getDisplayMetrics().density;
tv.setPadding(asDp(8, d), asDp(8, d), asDp(8, d), asDp(8, d));
setContentView(tv);
if (Build.VERSION.SDK_INT >= 11 &&
(state == null || !state.getBoolean(STATE_SAVED, false))) {
recreate();
} else {
new Handler().post(new Runnable() {
@Override
public void run() {
finish();
}
});
}
}
/**
* 系统是否在锁屏状态
*
* @return
*/
private boolean isScreenLocked()
{
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
return keyguardManager.inKeyguardRestrictedInputMode();
}
@Override
public void onReceive(final Context context, Intent intent) {
prefs = new Prefs(context);
prefs.apply();
this.context = context;
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
MainService.isScreenOn = false;
Globals.sensorIsScreenOff = true;
Utils.logInfo(TAG, "Screen turned off\nShown:" + Globals.isShown);
if (Globals.isShown && !MainService.isScreenOn) {
// Screen turned off with service running, wake up device
turnScreenOn(context, true);
} else {
//Checking if was killed by delay or naturally, if so, don't restart the service
if (Globals.killedByDelay) {
Globals.killedByDelay = false;
Utils.logDebug(SCREEN_RECEIVER_LOG_TAG, "Killed by delay and won't restart");
return;
}
// Start service when screen is off
if (!Globals.inCall && prefs.enabled) {
boolean toStart = shouldStart();
Utils.logDebug("SHOULD START ", String.valueOf(toStart));
if (toStart) {
if (prefs.startAfterLock) {
final KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (myKM.inKeyguardRestrictedInputMode()) {
//Screen is locked, start the service
Utils.logDebug(SCREEN_RECEIVER_LOG_TAG, "Device is locked");
context.startService(new Intent(context, MainService.class));
Globals.isShown = true;
} else {
//Screen is unlocked, wait until the lock timeout is over before starting the service.
int startDelay;
if (!doesDeviceHaveSecuritySetup(context)) {
Globals.noLock = true;
context.startService(new Intent(context, MainService.class));
Globals.isShown = true;
Utils.logDebug(SCREEN_RECEIVER_LOG_TAG, "Device is unlocked");
} else {
Utils.logDebug(SCREEN_RECEIVER_LOG_TAG, "Device is locked but has a timeout");
try {
startDelay = Settings.Secure.getInt(context.getContentResolver(), "lock_screen_lock_after_timeout", 5000);
if (startDelay == -1)
startDelay = (int) Settings.Secure.getLong(context.getContentResolver(), "lock_screen_lock_after_timeout", 5000);
Utils.logDebug(SCREEN_RECEIVER_LOG_TAG, "Lock time out " + String.valueOf(startDelay));
} catch (Exception settingNotFound) {
startDelay = 0;
}
if (startDelay > 0) {
new Handler().postDelayed(() -> {
if (myKM.inKeyguardRestrictedInputMode()) {
context.startService(new Intent(context, MainService.class));
Globals.isShown = true;
}
}, startDelay);
} else {
context.startService(new Intent(context, MainService.class));
Globals.isShown = true;
}
}
}
} else {
context.startService(new Intent(context, MainService.class));
Globals.isShown = true;
}
}
}
}
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
MainService.isScreenOn = true;
if (Globals.waitingForApp)
Globals.waitingForApp = false;
Utils.logInfo(TAG, "Screen turned on\nShown:" + Globals.isShown);
}
}
public static boolean isInLockScreen() {
KeyguardManager keyguardManager = (KeyguardManager) AppApplication.getContext().getSystemService(KEYGUARD_SERVICE);
return keyguardManager.inKeyguardRestrictedInputMode();
}
/**
* 判断是否锁屏
*
* @param context 上下文
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean isScreenLock(Context context) {
KeyguardManager km = (KeyguardManager) context
.getSystemService(Context.KEYGUARD_SERVICE);
return km.inKeyguardRestrictedInputMode();
}
/**
* 判断是否锁屏
*
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean isScreenLock(Context context) {
KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
return km.inKeyguardRestrictedInputMode();
}
/**
* 是否解锁
*
* @param context
* @return
*/
public static boolean isKeyguard(Context context) {
KeyguardManager mKeyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
boolean flag = mKeyguardManager.inKeyguardRestrictedInputMode();
return flag;
}
/**
* telephone is sleeping
*
* @param context
* @return
*/
public static boolean isSleeping(Context context) {
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
return keyguardManager.inKeyguardRestrictedInputMode();
}