现在新版的QQ Android客户端有个新功能,就是在锁屏界面也可以接收消息,并且接收到的消息可以显示在锁屏界面,双击之后可以进入QQ。
下面简单实现这个功能。当然正式项目中的运用还是非常复杂的,这仅仅是一个简单的Demo,可以作为参考:
直接上代码:
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TimerTask task = new TimerTask() {
@Override
public void run() {
Intent intent = new Intent();
intent.setAction("com.liang.test");
sendBroadcast(intent);
finish();
}
};
new Timer().schedule(task, 5000);
}
}
MessageReceiver.java
public class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
intent = new Intent(context, MessageActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
MessageActivity.java
public class MessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.activity_message);
findViewById(R.id.tv).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("");
keyguardLock.disableKeyguard();
}
}
效果图如下: