android锁屏(三) – 老牛比 – CSDN博客

来源: android锁屏(三) – 老牛比 – CSDN博客

锁屏服务类。1,启动后首先注册接受屏幕的关闭和开启的广播:广播中处理开屏锁屏任务;2,获取电话管理服务,注册通话状态监听:监听手机响铃状态、挂起状态、空闲状态。以Handler方式执行不同的任务。同样这里也是处理闹钟啊一些可以你认为可以在锁屏时候运行的东西。这里我没写,Dome而已,还是凑合看看吧

  1. import Android.app.KeyguardManager;
  2. import Android.app.Service;
  3. import Android.app.KeyguardManager.KeyguardLock;
  4. import android.content.BroadcastReceiver;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.IntentFilter;
  8. import android.os.Handler;
  9. import android.os.IBinder;
  10. import android.os.Message;
  11. import android.os.PowerManager;
  12. import android.telephony.PhoneStateListener;
  13. import android.telephony.TelephonyManager;
  14. import android.util.Log;
  15. public class LockService extends Service {
  16.     protected static final String TAG = “SAFLockService”;
  17.     KeyguardManager mKeyguardManager;
  18.     private KeyguardLock mKeyguardLock = null;
  19.     private TelephonyManager tm;
  20.     private Context mContext;
  21.     @Override
  22.     public void onCreate() {
  23.         super.onCreate();
  24.         mContext = this.getApplicationContext();
  25.         Log.v(TAG, “=============onCreate:================”);
  26.         IntentFilter filter = new IntentFilter();
  27.         filter.addAction(Intent.ACTION_SCREEN_OFF);
  28.         filter.addAction(Intent.ACTION_SCREEN_ON);
  29.         registerReceiver(mScreenReceiver, filter);
  30.         mKeyguardManager = (KeyguardManager) this
  31.                 .getSystemService(Context.KEYGUARD_SERVICE);
  32.         mKeyguardLock = mKeyguardManager.newKeyguardLock(“”);
  33.         mKeyguardLock.disableKeyguard();
  34.         tm = (TelephonyManager) this
  35.                 .getSystemService(Context.TELEPHONY_SERVICE);
  36.         SAFLockPhoneCallListener myPhoneCallListener = new SAFLockPhoneCallListener();
  37.         tm.listen(myPhoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);
  38.         SAFLockConstants.loadInitParam(this);
  39.         this.setForeground(true);
  40.     }
  41.     @Override
  42.     public IBinder onBind(Intent intent) {
  43.         return null;
  44.     }
  45.     @Override
  46.     public void onDestroy() {
  47.         super.onDestroy();
  48.         mKeyguardLock.reenableKeyguard();
  49.         mHandler.removeMessages(SAFLockConstants.EVENT_LOCK_SCREEN);
  50.         unregisterReceiver(mScreenReceiver);
  51.     }
  52.     /**
  53.      * set a listener to listen the phonestate,that we can do somthing by one
  54.      * of phonestates.
  55.      * 
  56.      * @author liao 
  57.      */
  58.     public class SAFLockPhoneCallListener extends PhoneStateListener {
  59.         public void onCallStateChanged(int state, String incomingNumber) {
  60.             switch (state) {
  61.             // Phone in idle
  62.             case TelephonyManager.CALL_STATE_IDLE:
  63.                 Log.v(TAG, “=============CALL_STATE_IDLE:================”);
  64.                 SAFLockConstants.PHONE_STATE = TelephonyManager.CALL_STATE_IDLE;
  65.                 if (SAFLockConstants.isLockecd) {
  66.                     mHandler.removeMessages(SAFLockConstants.EVENT_LOCK_SCREEN);
  67.                     mHandler.sendEmptyMessage(SAFLockConstants.EVENT_LOCK_SCREEN);
  68.                 }
  69.                 break;
  70.             // Phone is hung up
  71.             case TelephonyManager.CALL_STATE_OFFHOOK:
  72.                 Log.v(TAG, “=============CALL_STATE_OFFHOOK:================”);
  73.                 SAFLockConstants.PHONE_STATE = TelephonyManager.CALL_STATE_OFFHOOK;
  74.                 break;
  75.             // Cell phone ring
  76.             case TelephonyManager.CALL_STATE_RINGING:
  77.                 Log.v(TAG, “=============CALL_STATE_RINGING:================”);
  78.                 SAFLockConstants.PHONE_STATE = TelephonyManager.CALL_STATE_RINGING;
  79.                 break;
  80.             default:
  81.                 Log.v(TAG, “=============default:================”);
  82.                 break;
  83.             }
  84.         }
  85.     }
  86.     private Handler mHandler = new Handler() {
  87.         @Override
  88.         public void handleMessage(Message msg) {
  89.             switch (msg.what) {
  90.             case SAFLockConstants.EVENT_LOCK_SCREEN:
  91.                 // ====read user customization
  92.                 SAFLockConstants.updateLockIntent(
  93.                         “saf.cmcc.lock.THEME_DEFAULT”, mContext);
  94.                 // keep the task running 2s in sleeping mode.
  95.                 PowerManager pm = (PowerManager) mContext
  96.                         .getSystemService(Context.POWER_SERVICE);
  97.                 // Keep the CPU running, and the screen and keyboard light may
  98.                 // be closed.
  99.                 PowerManager.WakeLock mWakeLock = pm.newWakeLock(
  100.                         PowerManager.PARTIAL_WAKE_LOCK, “SAFLock”);
  101.                 mWakeLock.acquire(2000L);
  102.                 Intent mLockScreenIntent = new Intent(mContext,
  103.                         SAFFullScreenActivity.class);
  104.                 mLockScreenIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  105.                 mLockScreenIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
  106.                 mContext.startActivity(mLockScreenIntent);
  107.                 break;
  108.             default:
  109.                 break;
  110.             }
  111.         }
  112.     };
  113.     private BroadcastReceiver mScreenReceiver = new BroadcastReceiver() {
  114.         @Override
  115.         public void onReceive(Context context, Intent intent) {
  116.             String action = intent.getAction();
  117.             if (Intent.ACTION_SCREEN_OFF.equalsIgnoreCase(action)) {
  118.                 // load intent
  119.                 if (SAFLockConstants.LOCK_ENABLE
  120.                         && SAFLockConstants.PHONE_STATE == TelephonyManager.CALL_STATE_IDLE) {
  121.                     // =====send message after a short delay
  122.                     if (!SAFLockConstants.isLockecd) {
  123.                         mHandler.removeMessages(SAFLockConstants.EVENT_LOCK_SCREEN);
  124.                         mHandler.sendEmptyMessageDelayed(
  125.                                 SAFLockConstants.EVENT_LOCK_SCREEN, 100);
  126.                     }
  127.                 }
  128.             } else if (Intent.ACTION_SCREEN_ON.equalsIgnoreCase(action)) {
  129.                 // ====remove unprocessed event.
  130.                 mHandler.removeMessages(SAFLockConstants.EVENT_LOCK_SCREEN);
  131.             }
  132.         }
  133.     };
  134. }

注释是英文的,上头要求,没办法

分享到: 更多 (0)