android锁屏(三)

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

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;

	@Override
	public 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);
	}

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public 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 idle
			case 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 up
			case TelephonyManager.CALL_STATE_OFFHOOK:
				Log.v(TAG, "=============CALL_STATE_OFFHOOK:================");
				SAFLockConstants.PHONE_STATE = TelephonyManager.CALL_STATE_OFFHOOK;
				break;
			// Cell phone ring
			case 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() {

		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {

			case SAFLockConstants.EVENT_LOCK_SCREEN:

				// ====read user customization
				SAFLockConstants.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() {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();

			if (Intent.ACTION_SCREEN_OFF.equalsIgnoreCase(action)) {

				// load intent
				if (SAFLockConstants.LOCK_ENABLE
						&& SAFLockConstants.PHONE_STATE == TelephonyManager.CALL_STATE_IDLE) {

					// =====send message after a short delay
					if (!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);
			}
		}
	};
}


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

你可能感兴趣的:(android,String,service,animation,action)