下面列出了android.content.Intent#ACTION_USER_PRESENT 实例代码,或者点击链接到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();
}
@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();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// User present is usually sent after the device has been unlocked
IntentFilter userUnlock = new IntentFilter (Intent.ACTION_USER_PRESENT);
registerReceiver(unlockDone, userUnlock);
requestWindowFeature(android.view.Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Mlog.v(logTag, "creating dismiss window");
// In case we don't get an user present broadcast, just move on
handler.postDelayed(timeoutRunnable, 2000);
}
@Override
public void onReceive(Context context, Intent intent) {
try {
String action = intent.getAction();
// 打印当前触发的广播
LogPrintUtils.dTag(TAG, "onReceive Action: " + action);
// 判断类型
switch (action) {
case Intent.ACTION_SCREEN_ON: // 开屏
if (sListener != null) {
sListener.screenOn();
}
break;
case Intent.ACTION_SCREEN_OFF: // 锁屏
if (sListener != null) {
sListener.screenOff();
}
break;
case Intent.ACTION_USER_PRESENT: // 解锁
if (sListener != null) {
sListener.userPresent();
}
break;
}
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "onReceive");
}
}
private void registerScreenListeners() {
IntentFilter filterScreenOn = new IntentFilter(Intent.ACTION_SCREEN_ON);
IntentFilter filterScreenOff = new IntentFilter(Intent.ACTION_SCREEN_OFF);
IntentFilter filterUserUnlocked = new IntentFilter(Intent.ACTION_USER_PRESENT);
getApplication().registerReceiver(screenOnReceiver, filterScreenOn);
getApplication().registerReceiver(screenOffReceiver, filterScreenOff);
getApplication().registerReceiver(userUnlockedReceiver, filterUserUnlocked);
}
@Override
protected void onCreateTasks(Bundle savedInstanceState)
{
setContentView(R.layout.permissions);
mSettingsManager = CameraServicesImpl.instance().getSettingsManager();
// Filter for screen off so that we can finish permissions activity
// when screen is off.
IntentFilter filter_screen_off = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(mShutdownReceiver, filter_screen_off);
// Filter for phone unlock so that we can finish permissions activity
// via this UI path:
// 1. from secure lock screen, user starts secure camera
// 2. user presses home button
// 3. user unlocks phone
IntentFilter filter_user_unlock = new IntentFilter(Intent.ACTION_USER_PRESENT);
registerReceiver(mShutdownReceiver, filter_user_unlock);
Window win = getWindow();
if (isKeyguardLocked())
{
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
} else
{
win.clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
}
private void doRegister() {
if (mContext == null)
throw new NullPointerException("context is null");
if (activity == null)
throw new NullPointerException("target activity must nonnull");
Log.i(TAG, "已经注册广播");
mScreenStateBroadcast = new ScreenStateBroadcast();
IntentFilter mIntentFilter = new IntentFilter(Intent.ACTION_USER_PRESENT);
mIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
mContext.registerReceiver(mScreenStateBroadcast, mIntentFilter);
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent == null ? null : intent.getAction();
if (action == null)
action = Intent.ACTION_TIME_TICK;
switch (action) {
case Intent.ACTION_SCREEN_OFF: {
mCtx.unregisterReceiver(this);
mCtx.registerReceiver(this, mScreenOnOffFilter);
break;
}
case Intent.ACTION_SCREEN_ON: {
mCtx.unregisterReceiver(this);
mCtx.registerReceiver(this, mScreenOnOffFilter);
mCtx.registerReceiver(this, mTimeTickFilter);
mCtx.registerReceiver(this, mTimeChangedFilter);
}
case Intent.ACTION_USER_PRESENT:
case Intent.ACTION_TIME_CHANGED:
case Intent.ACTION_TIME_TICK:
default: {
int timeTick = (int) (System.currentTimeMillis() / 1000 / 60);
if (LAST_TIME_TICK != timeTick) {
InternalBroadcastReceiver.sender(mCtx).sendTimeTick();
LAST_TIME_TICK = timeTick;
}
break;
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
if (!TalkBackService.isServiceActive()) return;
String action = intent.getAction();
if (action == null) return;
EventId eventId = EVENT_ID_UNTRACKED; // Frequently not user-initiated.
switch (action) {
case AudioManager.RINGER_MODE_CHANGED_ACTION:
handleRingerModeChanged(
intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, AudioManager.RINGER_MODE_NORMAL));
break;
case Intent.ACTION_SCREEN_ON:
isScreenOn = true;
handleScreenOn(eventId);
break;
case Intent.ACTION_SCREEN_OFF:
isScreenOn = false;
handleScreenOff(eventId);
break;
case Intent.ACTION_USER_PRESENT:
handleDeviceUnlocked(eventId);
break;
}
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent == null ? null : intent.getAction();
if (action == null)
action = Intent.ACTION_TIME_TICK;
switch (action) {
case Intent.ACTION_SCREEN_OFF: {
mCtx.unregisterReceiver(this);
mCtx.registerReceiver(this, mScreenOnOffFilter);
break;
}
case Intent.ACTION_SCREEN_ON: {
mCtx.unregisterReceiver(this);
mCtx.registerReceiver(this, mScreenOnOffFilter);
mCtx.registerReceiver(this, mTimeTickFilter);
mCtx.registerReceiver(this, mTimeChangedFilter);
}
case Intent.ACTION_USER_PRESENT:
case Intent.ACTION_TIME_CHANGED:
case Intent.ACTION_TIME_TICK:
default: {
int timeTick = (int) (System.currentTimeMillis() / 1000 / 60);
if (LAST_TIME_TICK != timeTick) {
InternalBroadcastReceiver.sender(mCtx).sendTimeTick();
LAST_TIME_TICK = timeTick;
}
break;
}
}
}
@Override
public void onReceive(Context context, Intent intent) {
String event = null;
String type = null;
switch(intent.getAction()){
case Intent.ACTION_SCREEN_OFF:
event = DeviceEvent.EVENT_SCREEN_OFF;
type = DeviceEvent.TYPE_SCREEN;
break;
case Intent.ACTION_SCREEN_ON:
event = DeviceEvent.EVENT_SCREEN_ON;
type = DeviceEvent.TYPE_SCREEN;
break;
case Intent.ACTION_USER_PRESENT:
event = DeviceEvent.EVENT_SCREEN_USER_PRESENT;
type = DeviceEvent.TYPE_SCREEN;
break;
case Intent.ACTION_BOOT_COMPLETED:
event = DeviceEvent.EVENT_BOOT_COMPLETED;
type = DeviceEvent.TYPE_BOOT;
break;
case Intent.ACTION_SHUTDOWN:
event = DeviceEvent.EVENT_BOOT_SHUTDOWN;
type = DeviceEvent.TYPE_BOOT;
break;
case Intent.ACTION_BATTERY_LOW:
event = DeviceEvent.EVENT_BATTERY_LOW;
type = DeviceEvent.TYPE_BATTERY;
break;
case Intent.ACTION_BATTERY_OKAY:
event = DeviceEvent.EVENT_BATTERY_OKAY;
type = DeviceEvent.TYPE_BATTERY;
break;
case Intent.ACTION_POWER_CONNECTED:
event = DeviceEvent.EVENT_BATTERY_AC_CONNECTED;
type = DeviceEvent.TYPE_BATTERY;
break;
case Intent.ACTION_POWER_DISCONNECTED:
event = DeviceEvent.EVENT_BATTERY_AC_DISCONNECTED;
type = DeviceEvent.TYPE_BATTERY;
break;
case AudioManager.RINGER_MODE_CHANGED_ACTION:
AudioManager am = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
event = DeviceEvent.EVENT_RINGER_SILENT;
type = DeviceEvent.TYPE_RINGER;
break;
case AudioManager.RINGER_MODE_VIBRATE:
event = DeviceEvent.EVENT_RINGER_VIBRATE;
type = DeviceEvent.TYPE_RINGER;
break;
case AudioManager.RINGER_MODE_NORMAL:
event = DeviceEvent.EVENT_RINGER_NORMAL;
type = DeviceEvent.TYPE_RINGER;
break;
}
default:
break;
}
if (type != null)
output(new DeviceEvent(type, event));
}