Android development skills-imitation of the new version of QQ screen lock pop-up window
The new version of qq can display qq messages in the pop-up window under the screen lock. This function is also required for projects currently in progress. After a variety of experiments and data searches, the process is finally achieved, but there are some points to note.
The following is the implementation process.
1. When I use Activity instead of the pop-up window of ViewQQ, I thought it was a floating View. I used WindowManager to add it, but in any case it was not displayed. Later I changed it to Activity, the screen will pop up.
2. To set the Activity, you need to make the following settings to pop up the screen when the screen is locked. The first is the onCreate method. Four flags need to be added, as shown below:
Protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); final Window win = getWindow (); win. addFlags (WindowManager. layoutParams. FLAG_SHOW_WHEN_LOCKED | WindowManager. layoutParams. FLAG_DISMISS_KEYGUARD | WindowManager. layoutParams. FLAG_KEEP_SCREEN_ON | WindowManager. layoutParams. FLAG_TURN_SCREEN_ON); // your own code}
As the name suggests, the four signs are displayed in the screen lock status, unlock, keep the screen bright, and open the screen. In this way, when the Activity is started, it will be unlocked and displayed on the screen.
In the AndroidManifest. xml file, add the following attributes to the activity declaration:
For Layout files, the view to be displayed is centered and the background is transparent. As the background is set as the background of the wallpaper, the background of the desktop is displayed. If the background is set to the default white color, a white background appears behind the pop-up window and looks ugly. If the background is set to transparent, the unlocked page is displayed after the pop-up window (even if there is a screen Lock password, the unlocked page is displayed), which affects the visual effect.
3. Start the screen lock pop-up window in the broadcast. We set the pop-up window only when the screen is locked. It is not suitable for pop-up when the screen is not locked (you can try it, the effect will be quite strange ). Generally, you register a broadcast receiver and determine whether to display a window after receiving the specified broadcast. Therefore, in the BroadcastReceiver receiving code, you must first determine whether the screen is locked:
@Override public void onReceive(Context context, Intent intent) { Log.d(LOG_TAG, intent.getAction()); KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); if (km.inKeyguardRestrictedInputMode()) { Intent alarmIntent = new Intent(context, AlarmActivity.class); alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(alarmIntent); } }
The KeyguardManager class is used to manage the screen lock. After 4.1, the class API adds an isKeyguardLocked () method to determine whether to lock the screen. Before 4.1, we can only use inKeyguardRestrictedInputMode () method. If this parameter is set to true, the lock status is displayed. Note that the context of the Activity to be started in the broadcast may not be an Activity object, so you need to add the NEW_TASK flag. Otherwise, an error may be reported during startup.
4. update pop-up window information if the pop-up window Activity itself does not actively update information, when there is new information, you need to update the Activity interface, because we set the singleInstance startup mode above, therefore, you need to override the onNewIntent (Intent intent) method, so that when you start this activity again, the new intent will be passed in this method.
5. if the Activity does not exit but the screen lock key is manually pressed, the screen will not be aroused when the current broadcast receiver starts it again, therefore, we need to add the code to wake up the screen in the activity. The power lock is used here. It can be added to onNewIntent (Intent intent) because it will be called. You can also add other appropriate lifecycle methods. Add the following code:
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE); if (!pm.isScreenOn()) { PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright"); wl.acquire(); wl.release(); }
6. Some permissions are the permissions required in the implementation process. Because I have extracted them from the project code, it is inevitable to add or omit them. The developer should pay attention to them:
The first is needed to unlock the screen, and the second is required to apply for a power lock.
This article is original, reproduced please indicate the source: http://blog.csdn.net/maosidiaoxian/article/details/40587935
Reference: http://bbs.csdn.net/topics/390425777 CSDN Forum posts poster: JJMM2009android api documentation.