Recently I do a function, is to implement the Android program in the lock screen can continue to run, the author on the Internet to check some information, now sorted out, I hope to be helpful.
1. How to monitor screen lock screen
Monitor screen lock screen can be implemented as follows, directly through the code to determine, or through the listener to achieve
1) Use code to determine screen lock screen status
You can pass the PowerManager Isscreenon method with the following code:
PowerManager pm = (PowerManager) Context.getsystemservice (context.power_service); // If true, the screen is "lit", otherwise the screen is "dimmed". boolean Isscreenon = Pm.isscreenon ();
Here's what you need to explain:
The screen "lit" indicates that there are two states: A, unlocked screen B, is currently in the unlocked state. Both of these status screens are bright;
The screen is "dark", indicating that the screen is currently black.
By Keyguardmanager the Inkeyguardrestrictedinputmode method, can also realize the screen lock screen status
Keyguardmanager Mkeyguardmanager = (Keyguardmanager) Context.getsystemservice (context.keyguard_service); boolean flag = Mkeyguardmanager.inkeyguardrestrictedinputmode ();
A description of flag, tested and summarized as follows:
If flag is true, it indicates that there are two states: A, the screen is black B, is currently in the lock screen state.
If flag is false, it indicates that the screen is not currently locked
It is also possible to invoke the above two methods in a reflective way:
Method mreflectscreenstate; Try { = PowerManager. Classnew class[] {}); = (PowerManager)
Context.getsystemservice (Activity.power_service); boolean isscreenon=catch (Exception e) { e.printstacktrace ()
2) Use the listener to determine the screen's lock screen status
When the Android lock screen or screen is lit, or the screen is unlocked, the system will send the corresponding broadcast, we just need to listen to the broadcast.
The pseudo-code for registering the broadcast is as follows:
Screenbroadcastreceiver Mscreenreceiver; classScreenbroadcastreceiverextendsbroadcastreceiver {String action=NULL; @Override Public voidOnReceive (Context context, Intent Intent) {action=intent.getaction (); if(Intent.ACTION_SCREEN_ON.equals (ACTION)) {//Open Screen}Else if(Intent.ACTION_SCREEN_OFF.equals (ACTION)) {//Lock Screen}Else if(Intent.ACTION_USER_PRESENT.equals (ACTION)) {//Unlock } } } Private voidStartscreenbroadcastreceiver () {intentfilter filter=NewIntentfilter (); Filter.addaction (intent.action_screen_on); Filter.addaction (Intent.action_screen_off); Filter.addaction (intent.action_user_present); Context.registerreceiver (mscreenreceiver, filter); }
2. How to enable the phone screen to continue running after the lock screen
Above we know how to listen to the status of the screen, and then how to implement the screen after the program does not stop running. On this feature, I searched the internet for some information, the Android Wakelock mechanism is one of them, using the Android service can also. But after the author's test found that the effect is not ideal for Android version of the compatibility is not strong, the author's Android4.4 can support, but Android6.0 after testing found but not support.
The following author introduces a universal method, here thanks to the advice I put forward (I heard that QQ lock screen of the black technology it).
is when the screen lock screen, jump to another interface, the interface has only one pixel point.
The code is as follows:
Mainactivity.java
PackageOrg.screenlock.main;ImportJava.lang.reflect.Method;Importjava.util.Date;ImportJava.util.Timer;ImportJava.util.TimerTask;ImportCOM.EXAMPLE.SCREENLOCKTEST.R;ImportAndroid.os.Build;ImportAndroid.os.Bundle;ImportAndroid.os.PowerManager;ImportAndroid.os.PowerManager.WakeLock;Importandroid.app.Activity;ImportAndroid.app.KeyguardManager;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.content.IntentFilter;ImportAndroid.graphics.PixelFormat;ImportAndroid.text.format.Time;ImportAndroid.util.DisplayMetrics;ImportAndroid.util.Log;Importandroid.view.Gravity;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.WindowManager;ImportAndroid.view.WindowManager.LayoutParams;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity {Context Context=NULL; Intent mintent=NULL; StaticActivity mactivity=NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); This. Context = This; Mintent=NewIntent ( This, Singlepixelactivity.class); Startscreenbroadcastreceiver (Newscreenbroadcastreceiver ()); Timer Timer=NewTimer (); TimerTask TimerTask=NewTimerTask () {@Override Public voidrun () {LOG.I ("Info",NewDate (). toString ()); } }; Timer.schedule (TimerTask,0,10* 1000);//run every 10 seconds to see if the program is running } /*** Register screen broadcast*/ Private voidstartscreenbroadcastreceiver (Screenbroadcastreceiver mscreenreceiver) {intentfilter filter=NewIntentfilter (); Filter.addaction (intent.action_screen_on); Filter.addaction (Intent.action_screen_off); Filter.addaction (intent.action_user_present); Context.registerreceiver (mscreenreceiver, filter); } Private classScreenbroadcastreceiverextendsBroadcastreceiver {PrivateString action =NULL; @Override Public voidOnReceive (Context context, Intent Intent) {action=intent.getaction (); if(Intent.ACTION_SCREEN_ON.equals (ACTION)) {//Open ScreenLOG.I ("Info", "open screen"); Finishscreenactivity (); } Else if(Intent.ACTION_SCREEN_OFF.equals (ACTION)) {//Lock ScreenLOG.I ("info", "Lock Screen"); Startscreenactivity (); } Else if(Intent.ACTION_USER_PRESENT.equals (ACTION)) {//UnlockLOG.I ("info", "solution screen"); } } } /*** Turn off screen lock screen*/ Private voidfinishscreenactivity () {if(mactivity!=NULL) {mactivity.finish (); } } /*** Jump to screen lock screen*/ Private voidstartscreenactivity () {startactivity (mintent); }}
Mainactivity.java
Singlepixelactivity.java
PackageOrg.screenlock.main;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.Window;ImportAndroid.view.WindowManager; Public classSinglepixelactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Window Window=GetWindow (); Windowmanager.layoutparams params=NewWindowmanager.layoutparams (); Params.x=0; Params.y=0; Params.width=1; Params.height=1; Window.setattributes (params); Mainactivity.mactivity= This; }}
Singlepixelactivity.java
The androidmanifest.xml needs to be added as follows:
<ActivityAndroid:name= "Org.screenlock.main.SinglePixelActivity"> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </Intent-filter> </Activity>
"Android" How to implement Android program to continue running after the mobile phone lock screen