Lock screen service. 1. Register and accept the closed and open broadcasting of the screen after startup: process the screen-opening and screen-locking tasks in the broadcast; 2. Obtain the telephone management service and register the call status listener: listen to the phone's bell status, suspension status, and idle status. Execute different tasks in handler mode. This is also the way to handle the alert clock, something you think can run during screen lock. I didn't write it here, just dome. Let's take a look.
import android.app.KeyguardManager;import android.app.Service;import android.app.KeyguardManager.KeyguardLock;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Handler;import android.os.IBinder;import android.os.Message;import android.os.PowerManager;import android.telephony.PhoneStateListener;import android.telephony.TelephonyManager;import android.util.Log;public class LockService extends Service {protected static final String TAG = "SAFLockService";KeyguardManager mKeyguardManager;private KeyguardLock mKeyguardLock = null;private TelephonyManager tm;private Context mContext;@Overridepublic void onCreate() {super.onCreate();mContext = this.getApplicationContext();Log.v(TAG, "=============onCreate:================");IntentFilter filter = new IntentFilter();filter.addAction(Intent.ACTION_SCREEN_OFF);filter.addAction(Intent.ACTION_SCREEN_ON);registerReceiver(mScreenReceiver, filter);mKeyguardManager = (KeyguardManager) this.getSystemService(Context.KEYGUARD_SERVICE);mKeyguardLock = mKeyguardManager.newKeyguardLock("");mKeyguardLock.disableKeyguard();tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);SAFLockPhoneCallListener myPhoneCallListener = new SAFLockPhoneCallListener();tm.listen(myPhoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);SAFLockConstants.loadInitParam(this);this.setForeground(true);}@Overridepublic IBinder onBind(Intent intent) {return null;}@Overridepublic void onDestroy() {super.onDestroy();mKeyguardLock.reenableKeyguard();mHandler.removeMessages(SAFLockConstants.EVENT_LOCK_SCREEN);unregisterReceiver(mScreenReceiver);}/** * set a listener to listen the phonestate,that we can do somthing by one * of phonestates. * * @author liao */public class SAFLockPhoneCallListener extends PhoneStateListener {public void onCallStateChanged(int state, String incomingNumber) {switch (state) {// Phone in idlecase TelephonyManager.CALL_STATE_IDLE:Log.v(TAG, "=============CALL_STATE_IDLE:================");SAFLockConstants.PHONE_STATE = TelephonyManager.CALL_STATE_IDLE;if (SAFLockConstants.isLockecd) {mHandler.removeMessages(SAFLockConstants.EVENT_LOCK_SCREEN);mHandler.sendEmptyMessage(SAFLockConstants.EVENT_LOCK_SCREEN);}break;// Phone is hung upcase TelephonyManager.CALL_STATE_OFFHOOK:Log.v(TAG, "=============CALL_STATE_OFFHOOK:================");SAFLockConstants.PHONE_STATE = TelephonyManager.CALL_STATE_OFFHOOK;break;// Cell phone ringcase TelephonyManager.CALL_STATE_RINGING:Log.v(TAG, "=============CALL_STATE_RINGING:================");SAFLockConstants.PHONE_STATE = TelephonyManager.CALL_STATE_RINGING;break;default:Log.v(TAG, "=============default:================");break;}}}private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case SAFLockConstants.EVENT_LOCK_SCREEN:// ====read user customizationSAFLockConstants.updateLockIntent("saf.cmcc.lock.THEME_DEFAULT", mContext);// keep the task running 2s in sleeping mode.PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);// Keep the CPU running, and the screen and keyboard light may// be closed.PowerManager.WakeLock mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SAFLock");mWakeLock.acquire(2000L);Intent mLockScreenIntent = new Intent(mContext,SAFFullScreenActivity.class);mLockScreenIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);mLockScreenIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);mContext.startActivity(mLockScreenIntent);break;default:break;}}};private BroadcastReceiver mScreenReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (Intent.ACTION_SCREEN_OFF.equalsIgnoreCase(action)) {// load intentif (SAFLockConstants.LOCK_ENABLE&& SAFLockConstants.PHONE_STATE == TelephonyManager.CALL_STATE_IDLE) {// =====send message after a short delayif (!SAFLockConstants.isLockecd) {mHandler.removeMessages(SAFLockConstants.EVENT_LOCK_SCREEN);mHandler.sendEmptyMessageDelayed(SAFLockConstants.EVENT_LOCK_SCREEN, 100);}}} else if (Intent.ACTION_SCREEN_ON.equalsIgnoreCase(action)) {// ====remove unprocessed event.mHandler.removeMessages(SAFLockConstants.EVENT_LOCK_SCREEN);}}};}
The comments are in English. The above requirements are not supported.