一般的社交类聊天应用都会有在锁定屏幕状态下弹出消息的需求,以下总结一下:
1、新建Activity,全屏,锁屏可用。
方法,在Oncreate中增加一下代码
getWindow().addFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED // | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
2、在后台收到消息的地方启动此activity,如
KeyguardManager km = (KeyguardManager) mContext
.getSystemService(Context.KEYGUARD_SERVICE);
if (km != null && km.inKeyguardRestrictedInputMode()) {
Intent intent = new Intent(mContext,
MessageOnLockedScreenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(MessageOnLockedScreenActivity.class
.getName(),
(ArrayList<ChatMessageModel>) messageSessionModel
.getNormalMessages());
mContext.startActivity(intent);
}
3、声明权限
<uses-permission android:name="android.permission.WAKE_LOCK" />