On-screen, screen lock, and screen unlocking events of the Android system (after some mobile phones are on, they enter the resume status if they are not unlocked)

Source: Internet
Author: User


Some SamSung mobile phones close the screen, just open the screen, the foreground activity will resume status, because we use the cocos2d-x engine, the default resume and activity resume event is consistent, we re-open the closed Sound in resume, so after the user lights up the screen (not unlocked ), you will hear in-game sound (if our game is on the frontend when the screen is off ).


To solve this problem, we need to modify the trigger time of our resume. Make sure that the game enters the resume status after unlocking. So when should our game enter the resume status? When the system activity enters the resume status, and the screen is bright, the screen is not locked The screen has just been on and the screen has not been locked. The system activity has entered the resume status. Unlock the screen. The screen must be bright and the activity has entered the resume status.
With the above three points, it is not difficult to believe that the logic code is written. The following describes how to harden the system's screen, enable the screen, and unlock the screen, and how to determine whether the current screen is unlocked (because the user can set whether to lock the screen after the screen is closed, the unlock screen event may not be triggered at all, therefore, we cannot identify the status by tracking events)
To ensure that the screen is bright, the screen is off, and the screen is unlocked, We need to register and listen to three actions in the Code: ACTION_SCREEN_ON, ACTION_SCREEN_OFF, and ACTION_USER_PRESENT. The specific statement is as follows:
/*** Screen status broadcast receiver */private class ScreenBroadcastReceiver extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {if (Intent. ACTION_SCREEN_ON.equals (intent. getAction () {mScreenStateListener. onScreenOn ();} else if (Intent. ACTION_SCREEN_OFF.equals (intent. getAction () {mScreenStateListener. onScreenOff ();} else if (Intent. ACTION_USER_PRESENT.equals (intent. getAction () {mScreenStateListener. onUserPresent ();}}}

/*** Stop screen status update */public void stopScreenStateUpdate () {mContext. unregisterReceiver (mScreenReceiver);}/*** Start screen status broadcast receiver */private void startScreenBroadcastReceiver () {IntentFilter filter = new IntentFilter (); filter. addAction (Intent. ACTION_SCREEN_ON); filter. addAction (Intent. ACTION_SCREEN_OFF); filter. addAction (Intent. ACTION_USER_PRESENT); mContext. registerReceiver (mScreenReceiver, filter );}


Because the user can set whether the screen is locked after it is disabled, we cannot track the unlock event to determine its status. A function that can obtain the status from time to time is required:
// Determine whether the screen is locked public final static boolean isScreenLocked (Context c) {android. app. keyguardManager mKeyguardManager = (KeyguardManager) c. getSystemService (c. KEYGUARD_SERVICE); return mKeyguardManager. inKeyguardRestrictedInputMode ();}

Note that most events can be stored in AndroidManifest. but the ACTION_SCREEN_ON, ACTION_SCREEN_OFF, and ACTION_USER_PRESENT events are restricted on the PowerManager side. You must register them in the Code as above before you can listen to them, so don't step on it ~~~
The following is a class code for packaging screen events for your reference only:
Package com. example. myfirstapp; import java. lang. reflect. method; import android. app. activity; import android. app. keyguardManager; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. content. intentFilter; import android. OS. powerManager; import android. util. log;/*** listen to screen ON and off present statuses ** @ author * @ 2014 **/public class ScreenObserver {pr Ivate static String TAG = "ScreenObserver"; private Context mContext; private extends mScreenReceiver; private ScreenStateListener mScreenStateListener; private static Method mReflectScreenState; public ScreenObserver (Context context) {mContext = context; mScreenReceiver = new ScreenBroadcastReceiver (); try {mReflectScreenState = PowerManager. class. getMethod ("isScreenOn", new Class [] {});} Catch (Exception nsme) {Log. d (TAG, "API <7," + nsme) ;}/ *** screen status broadcast receiver */private class ScreenBroadcastReceiver extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, intent intent) {if (Intent. ACTION_SCREEN_ON.equals (intent. getAction () {mScreenStateListener. onScreenOn ();} else if (Intent. ACTION_SCREEN_OFF.equals (intent. getAction () {mScreenStateListener. onScreenOff ();} Else if (Intent. ACTION_USER_PRESENT.equals (intent. getAction () {mScreenStateListener. onUserPresent () ;}}/ *** request screen status update */public void requestScreenStateUpdate (ScreenStateListener listener) {mScreenStateListener = listener; listener (); firstGetScreenState ();} /*** screen status of the first request */private void firstGetScreenState () {PowerManager manager = (PowerManager) mContext. getSystemServi Ce (Activity. POWER_SERVICE); if (isScreenOn (manager) {if (mScreenStateListener! = Null) {mScreenStateListener. onScreenOn () ;}} else {if (mScreenStateListener! = Null) {mScreenStateListener. onScreenOff () ;}}/ *** stop screen status update */public void stopScreenStateUpdate () {mContext. unregisterReceiver (mScreenReceiver);}/*** Start screen status broadcast receiver */private void startScreenBroadcastReceiver () {IntentFilter filter = new IntentFilter (); filter. addAction (Intent. ACTION_SCREEN_ON); filter. addAction (Intent. ACTION_SCREEN_OFF); filter. addAction (Intent. ACTION_USER_PRESENT); mContext. registerReceiver (mScreenReceiver, filter);}/*** screen enabled? */private static boolean isScreenOn (PowerManager pm) {boolean screenState; try {screenState = (Boolean) mReflectScreenState. invoke (pm);} catch (Exception e) {screenState = false;} return screenState;} // public interface ScreenStateListener {public void onScreenOn (); public void onScreenOff (); public void onUserPresent ();} public final static boolean isScreenLocked (Context c) {android. app. keyguardManager mKeyguardManager = (KeyguardManager) c. getSystemService (c. KEYGUARD_SERVICE); return mKeyguardManager. inKeyguardRestrictedInputMode ();}}


See http://blog.csdn.net/m_changgong/article/details/7608911.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.