转载自 :
前言:在Android系统锁屏模块开发中,有两种形式的开发:一种是基于源码开发;另一种是基于第三方开发。基于源码开发还是比较具有危险性的,相对于第三方开发来说要考虑的问题比较多,而且移植后可能会出现问题,不便于维护。
当然,基于源码开发锁屏要做的工作相对于第三方开发锁屏要做的工作就少了很多,比如在时间和日期的获取和实时更新、充电时显示的电量状态、运营商名字的显示等等。这些在源码中是已经开发好了,直接可以调用实现。
而基于第三方锁屏的开发就要考虑这些问题,而且还要考虑在启动第三方锁屏时如何屏蔽系统原生的锁屏。
最近鄙人在参考其他人已有的成果上,自己尝试着去开发一个基于第三方锁屏Demo。锁屏相关的资源图片都是自己网上找的,算是仿360的星空锁屏吧。也没有创新性,基本上还是典型的中心图标绕圆圈实现解锁或启动其他应用。现在就把效果图和代码贴出来给大家吐槽一下。
一. 星空锁屏功能介绍:
1. 解锁、启动拨号、短信、相机界面和功能介绍:
功能实现介绍:
-->中心图标动画显示,用户点中心图标可任意拖动。
--> 无论用户拖动中心图标时,手指移动到屏幕的任意一处,中心图标都保持在一定的圆圈范围内移动。
-->若用户拖动中心图标到下方的解锁小图标区域,此时显示白色高亮小图标。用户手指放开时,手机将解锁进入主界面。
-->若用户拖动中心图标到上方的相机小图标区域时,此时显示白色高亮小图标。用户手指放开时,手机将启动相机应用。
-->若用户拖动中心图标到左方的拨号小图标区域时,此时显示白色高亮小图标。用户手指放开时,手机将启动拨号界面。
-->若用户拖动中心图标到右方的短信小图标区域时,此时显示白色高亮小图标。用户手指放开时,手机将启动短信应用。
2. 双击可拖动的中心图标显示的音乐播放控制界面和功能介绍:
功能实现介绍:
-->在显示音乐播放控件界面的情况下,用户拖动中心图标时,隐藏音乐控制按钮,显示解锁、启动应用相关图标,进行解锁、启动应用操作。
-->用户可点击上方的音乐控制按钮图标,进行音乐的播放、暂停。(音乐正在播放时显示暂停按钮,暂停时显示播放按钮)
-->用户可点击下方的音乐控制按钮图标,进行音乐的停止。(音乐停止时,上方图标显示音乐播放按钮)
-->用户可点击左方的音乐控制按钮图标,进行上一曲播放(若音乐在暂时状态时点击上一曲,则上方图标显示暂停按钮)
-->用户可点击右方的音乐控制按钮图标,进行下一曲播放(若音乐在暂时状态时点击下一曲,则上方图标显示暂停按钮)
-->当锁屏界面双击切换到控制音乐播放界面时,上方界面日期显示下方会显示正在播放的歌曲相关信息(歌曲、歌手)
-->当正在处于音乐控制界面时,再次双击中心图标,回到原来的锁屏操作界面(依次循环)
二. 星空锁屏代码实现:
1. main.xml:
该布局文件为显示锁屏全局界面的布局,代码如下:
2. StarLockView.java:
该自定义ViewGroup类为锁屏主要功能实现的核心类。该类负责处理中心图标的动画显示、用户拖动监听操作。同时也处理了解锁图标、应用程序启动图标、音乐控制图标等相关小图标的布局、监听。实现锁屏界面和音乐控制界面的显示和切换,实现发送广播至系统音乐应用的后台服务(MediaPlaybackService),已达到控制后台音乐服务。代码如下:
- package com.phicomm.hu;
-
- import com.phicomm.hu.R;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Rect;
- import android.os.Handler;
- import android.os.Vibrator;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.View.OnClickListener;
- import android.view.animation.AlphaAnimation;
- import android.view.animation.Animation;
- import android.view.animation.TranslateAnimation;
- import android.widget.ImageView;
- import android.widget.ImageView.ScaleType;
-
- public class StarLockView extends ViewGroup implements OnClickListener
- {
-
- private static final boolean DBG = true;
- private Context mContext;
- private Handler mainHandler = null;
- private float mx;
- private float my;
- private int count = 0;
- private long firstClick = 0;
- private long secondClick = 0;
-
- private int mWidth, mHight;
- private int mScreenHalfWidth;
- private int mAlphaViewWidth, mAlphaViewHeight;
- private int mCenterViewWidth, mCenterViewHeight;
- private int mCenterViewTop, mCenterViewBottom;
- private int mAlphaViewTop, mAlphaViewBottom;
- private int mSmsViewHalfWidth, mSmsViewHalfHeight;
- private int mDialViewHalfWidth, mDialViewHalfHeight;
- private int mCameraViewHalfWidth, mHalfCameraViewHeight;
- private int mUnlockViewHalfWidth, mUnlockViewHalfHeight;
- private int mLightViewHalfWidth, mLightViewHalfHeight;
- private int mMusicViewHalfWidth, mMusicViewHalfHeight;
-
- private ImageView mSmsView, mDialView, mCameraView, mUnLockView;
- private ImageView mCenterView, mAlphaView;
- private ImageView mSmsLightView, mUnLockLightView,
- mCameraLightView, mDialLightView;
-
- private ImageView mPlayView, mNextView, mPrevView, mStopView;
-
- private Rect smsRect, dialRect, cameraRect, unlockRect;
- private Rect mCenterViewRect;
-
- private AlphaAnimation alpha;
- private boolean mTracking = false;
-
- private static final String TAG = "FxLockView";
- public static final String SHOW_MUSIC = "com.phicomm.hu.action.music";
- private static final String SERVICECMD = "com.android.music.musicservicecommand";
- private static final String CMDNAME = "command";
- private static final String CMDSTOP = "stop";
- private static final String CMDPAUSE = "pause";
- private static final String CMDPLAY = "play";
- private static final String CMDPREV = "previous";
- private static final String CMDNEXT = "next";
-
- public StarLockView(Context context, AttributeSet attrs) {
- super(context, attrs, 0);
- mContext = context;
- if(DBG) Log.d(TAG, "FxLockView2");
-
- if(DBG) Log.d(TAG, "FxLockView-->isMusic--->" + MusicInfo.isMusic());
- MusicInfo.setMusic(false);
- initViews(context);
- setViewId();
-
- onAnimationStart();
- }
-
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
-
- if (changed) {
- mWidth = r;
- mHight = b;
-
- mScreenHalfWidth = mWidth >> 1;
-
- getViewMeasure();
- mCenterViewTop = 4 * mHight / 7 - (mCenterViewHeight >> 1);
- mCenterViewBottom = 4 * mHight / 7 + (mCenterViewHeight >> 1);
- mAlphaViewTop = 4 * mHight / 7 - (mAlphaViewHeight >> 1);
- mAlphaViewBottom = 4 * mHight / 7 + (mAlphaViewHeight >> 1);
-
- setChildViewLayout();
- setMusicButtonsLayout();
- setActivatedViewLayout();
- getChildViewRect();
-
- mCenterViewRect = new Rect(mWidth / 2 - mAlphaViewWidth / 2, mAlphaViewTop,
- mWidth / 2 + mAlphaViewWidth / 2, mAlphaViewBottom);
-
- }
-
- if(DBG) Log.d(TAG, "l-->" + l);
- if(DBG) Log.d(TAG, "t-->" + t);
- if(DBG) Log.d(TAG, "r-->" + r);
- if(DBG) Log.d(TAG, "b-->" + b);
- }
-
-
- private void getViewMeasure()
- {
- mAlphaView.measure(View.MeasureSpec.makeMeasureSpec(0,
- View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
- .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
- mAlphaViewWidth = mAlphaView.getMeasuredWidth();
- mAlphaViewHeight = mAlphaView.getMeasuredHeight();
-
- mCenterView.measure(View.MeasureSpec.makeMeasureSpec(0,
- View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
- .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
- mCenterViewWidth = mCenterView.getMeasuredWidth();
- mCenterViewHeight = mCenterView.getMeasuredHeight();
-
- mSmsView.measure(View.MeasureSpec.makeMeasureSpec(0,
- View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
- .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
- mSmsViewHalfWidth = (mSmsView.getMeasuredWidth()) >> 1;
- mSmsViewHalfHeight = (mSmsView.getMeasuredHeight()) >> 1;
-
- mDialView.measure(View.MeasureSpec.makeMeasureSpec(0,
- View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
- .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
- mDialViewHalfWidth = (mDialView.getMeasuredWidth()) >> 1;
- mDialViewHalfHeight = (mDialView.getMeasuredHeight()) >> 1;
-
- mCameraView.measure(View.MeasureSpec.makeMeasureSpec(0,
- View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
- .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
- mCameraViewHalfWidth = (mCameraView.getMeasuredWidth()) >> 1;
- mHalfCameraViewHeight = (mCameraView.getMeasuredHeight()) >> 1;
-
- mUnLockView.measure(View.MeasureSpec.makeMeasureSpec(0,
- View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
- .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
- mUnlockViewHalfWidth = (mUnLockView.getMeasuredWidth()) >> 1;
- mUnlockViewHalfHeight = (mUnLockView.getMeasuredHeight()) >> 1;
-
- mSmsLightView.measure(View.MeasureSpec.makeMeasureSpec(0,
- View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
- .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
- mLightViewHalfWidth = (mSmsLightView.getMeasuredWidth()) >> 1;
- mLightViewHalfHeight = (mSmsLightView.getMeasuredHeight()) >> 1;
-
- mPlayView.measure(View.MeasureSpec.makeMeasureSpec(0,
- View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
- .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
- mMusicViewHalfWidth = (mPlayView.getMeasuredWidth()) >> 1;
- mMusicViewHalfHeight = (mPlayView.getMeasuredHeight()) >> 1;
- }
-
-
- private void setActivatedViewLayout()
- {
- mUnLockLightView.layout(mScreenHalfWidth - mLightViewHalfWidth - 5,
- (mCenterViewTop + 2 * mCenterViewHeight) - mLightViewHalfHeight,
- mScreenHalfWidth + mLightViewHalfWidth - 5,
- (mCenterViewBottom + mCenterViewHeight) + mLightViewHalfHeight);
- mSmsLightView.layout((mScreenHalfWidth + 3 * mCenterViewWidth / 2) - 2 * mLightViewHalfWidth,
- (mCenterViewTop + mCenterViewHeight / 2) - mLightViewHalfHeight,
- (mScreenHalfWidth + 3 * mCenterViewWidth / 2) + 2 * mLightViewHalfWidth,
- (mAlphaViewBottom - mCenterViewHeight / 2) + mLightViewHalfHeight);
- mDialLightView.layout((mScreenHalfWidth - 3 * mCenterViewWidth / 2) - mLightViewHalfWidth,
- (mCenterViewTop + mCenterViewHeight / 2) - mLightViewHalfHeight,
- (mScreenHalfWidth - 3 * mCenterViewWidth / 2) + mLightViewHalfWidth,
- (mAlphaViewBottom - mCenterViewHeight / 2) + mLightViewHalfHeight);
- mCameraLightView.layout(mScreenHalfWidth - mLightViewHalfWidth,
- (mCenterViewTop - mCenterViewHeight) - mLightViewHalfHeight,
- mScreenHalfWidth + mLightViewHalfWidth,
- (mCenterViewBottom - 2 * mCenterViewHeight) + mLightViewHalfHeight);
- }
-
-
- private void setChildViewLayout()
- {
- mAlphaView.layout(mScreenHalfWidth - mAlphaViewWidth / 2, mAlphaViewTop,
- mScreenHalfWidth + mAlphaViewWidth / 2, mAlphaViewBottom);
-
- mCenterView.layout(mScreenHalfWidth - mCenterViewWidth / 2, mCenterViewTop,
- mScreenHalfWidth + mCenterViewWidth / 2, mCenterViewBottom);
-
- mSmsView.layout((mScreenHalfWidth + 3 * mCenterViewWidth / 2) - 2 * mSmsViewHalfWidth,
- (mCenterViewTop + mCenterViewHeight / 2) - mSmsViewHalfHeight,
- (mScreenHalfWidth + 3 * mCenterViewWidth / 2) + 2 * mSmsViewHalfWidth,
- (mAlphaViewBottom - mCenterViewHeight / 2) + mSmsViewHalfHeight);
-
- mDialView.layout((mScreenHalfWidth - 3 * mCenterViewWidth / 2) - mDialViewHalfWidth,
- (mCenterViewTop + mCenterViewHeight / 2) - mDialViewHalfHeight,
- (mScreenHalfWidth - 3 * mCenterViewWidth / 2) + mDialViewHalfWidth,
- (mAlphaViewBottom - mCenterViewHeight / 2) + mDialViewHalfHeight);
-
- mCameraView.layout(mScreenHalfWidth - mCameraViewHalfWidth,
- (mCenterViewTop - mCenterViewHeight) - mHalfCameraViewHeight,
- mScreenHalfWidth + mCameraViewHalfWidth,
- (mCenterViewBottom - 2 * mCenterViewHeight) + mHalfCameraViewHeight);
-
- mUnLockView.layout(mScreenHalfWidth - mUnlockViewHalfWidth,
- (mCenterViewTop + 2 * mCenterViewHeight) - mUnlockViewHalfHeight,
- mScreenHalfWidth + mUnlockViewHalfWidth,
- (mCenterViewBottom + mCenterViewHeight) + mUnlockViewHalfHeight);
-
- }
-
-
- private void setMusicButtonsLayout()
- {
- mNextView.layout((mScreenHalfWidth + 3 * mCenterViewWidth / 2) - 2 * mMusicViewHalfWidth,
- (mCenterViewTop + mCenterViewHeight / 2) - mMusicViewHalfHeight,
- (mScreenHalfWidth + 3 * mCenterViewWidth / 2) + 2 * mMusicViewHalfWidth,
- (mAlphaViewBottom - mCenterViewHeight / 2) + mMusicViewHalfHeight);
-
- mPrevView.layout((mScreenHalfWidth - 3 * mCenterViewWidth / 2) - mMusicViewHalfWidth,
- (mCenterViewTop + mCenterViewHeight / 2) - mMusicViewHalfHeight,
- (mScreenHalfWidth - 3 * mCenterViewWidth / 2) + mMusicViewHalfWidth,
- (mAlphaViewBottom - mCenterViewHeight / 2) + mMusicViewHalfHeight);
-
- mStopView.layout(mScreenHalfWidth - mMusicViewHalfWidth,
- (mCenterViewTop + 2 * mCenterViewHeight) - mMusicViewHalfHeight,
- mScreenHalfWidth + mMusicViewHalfWidth,
- (mCenterViewBottom + mCenterViewHeight) + mMusicViewHalfHeight);
-
- mPlayView.layout(mScreenHalfWidth - mMusicViewHalfWidth,
- (mCenterViewTop - mCenterViewHeight) - mMusicViewHalfHeight,
- mScreenHalfWidth + mMusicViewHalfWidth,
- (mCenterViewBottom - 2 * mCenterViewHeight) + mMusicViewHalfHeight);
- }
-
-
- private void getChildViewRect()
- {
- smsRect = new Rect((mScreenHalfWidth + 3 * mCenterViewWidth / 2) - 2 * mSmsViewHalfWidth,
- (mCenterViewTop + mCenterViewHeight / 2) - mSmsViewHalfHeight,
- (mScreenHalfWidth + 3 * mCenterViewWidth / 2) + 2 * mSmsViewHalfWidth,
- (mAlphaViewBottom - mCenterViewHeight / 2) + mSmsViewHalfHeight);
-
- dialRect = new Rect((mScreenHalfWidth - 3 * mCenterViewWidth / 2) - mDialViewHalfWidth,
- (mCenterViewTop + mCenterViewHeight / 2) - mDialViewHalfHeight,
- (mScreenHalfWidth - 3 * mCenterViewWidth / 2) + mDialViewHalfWidth,
- (mAlphaViewBottom - mCenterViewHeight / 2) + mDialViewHalfHeight);
-
- cameraRect = new Rect(mScreenHalfWidth - mCameraViewHalfWidth,
- (mCenterViewTop - mCenterViewHeight) - mHalfCameraViewHeight,
- mScreenHalfWidth + mCameraViewHalfWidth,
- (mCenterViewBottom - 2 * mCenterViewHeight) + mHalfCameraViewHeight);
-
- unlockRect = new Rect(mScreenHalfWidth - mUnlockViewHalfWidth,
- (mCenterViewTop + 2 * mCenterViewHeight) - mUnlockViewHalfHeight,
- mScreenHalfWidth + mUnlockViewHalfWidth,
- (mCenterViewBottom + mCenterViewHeight) + mUnlockViewHalfHeight);
- }
-
-
- private void initViews(Context context) {
- mAlphaView = new ImageView(context);
- mAlphaView.setImageResource(R.drawable.centure2);
- setViewsLayout(mAlphaView);
- mAlphaView.setVisibility(View.INVISIBLE);
-
- mCenterView = new ImageView(context);
- mCenterView.setImageResource(R.drawable.centure1);
- setViewsLayout(mCenterView);
- mCenterView.setVisibility(View.VISIBLE);
-
- mSmsView = new ImageView(context);
- mSmsView.setImageResource(R.drawable.sms);
- setViewsLayout(mSmsView);
- mSmsView.setVisibility(View.VISIBLE);
-
- mDialView = new ImageView(context);
- mDialView.setImageResource(R.drawable.dial);
- setViewsLayout(mDialView);
- mDialView.setVisibility(View.VISIBLE);
-
- mCameraView = new ImageView(context);
- mCameraView.setImageResource(R.drawable.camera);
- setViewsLayout(mCameraView);
- mCameraView.setVisibility(View.VISIBLE);
-
- mUnLockView = new ImageView(context);
- mUnLockView.setImageResource(R.drawable.home);
- setViewsLayout(mUnLockView);
- mUnLockView.setVisibility(View.VISIBLE);
-
- mNextView = new ImageView(context);
- mNextView.setImageResource(R.drawable.next);
- setViewsLayout(mNextView);
- setMusicButtonBackground(mNextView);
-
- mPrevView = new ImageView(context);
- mPrevView.setImageResource(R.drawable.prev);
- setViewsLayout(mPrevView);
- setMusicButtonBackground(mPrevView);
-
- mPlayView = new ImageView(context);
- setPlayViewDrawable();
- setViewsLayout(mPlayView);
- setMusicButtonBackground(mPlayView);
-
- mStopView = new ImageView(context);
- mStopView.setImageResource(R.drawable.stop);
- setViewsLayout(mStopView);
- setMusicButtonBackground(mStopView);
-
-
- mSmsLightView= new ImageView(context);
- setLightDrawable(mSmsLightView);
- setViewsLayout(mSmsLightView);
- mSmsLightView.setVisibility(INVISIBLE);
-
- mUnLockLightView = new ImageView(context);
- setLightDrawable(mUnLockLightView);
- setViewsLayout(mUnLockLightView);
- mUnLockLightView.setVisibility(INVISIBLE);
-
- mCameraLightView = new ImageView(context);
- setLightDrawable(mCameraLightView);
- setViewsLayout(mCameraLightView);
- mCameraLightView.setVisibility(INVISIBLE);
-
- mDialLightView = new ImageView(context);
- setLightDrawable(mDialLightView);
- setViewsLayout(mDialLightView);
- mDialLightView.setVisibility(INVISIBLE);
- }
-
- private void setLightDrawable(ImageView img)
- {
- img.setImageResource(R.drawable.light);
- }
-
-
- private void setViewsLayout(ImageView image) {
- image.setScaleType(ScaleType.CENTER);
- image.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
- LayoutParams.WRAP_CONTENT));
- addView(image);
- }
-
-
- private void setMusicButtonBackground(ImageView musicIcon)
- {
- musicIcon.setBackgroundResource(R.drawable.music_button_bg);
- musicIcon.setVisibility(View.INVISIBLE);
- }
-
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
-
- final int action = ev.getAction();
- final float x = ev.getX();
- final float y = ev.getY();
-
- switch (action) {
- case MotionEvent.ACTION_DOWN:
-
- if (mCenterViewRect.contains((int) x, (int) y))
- {
- mTracking = true;
-
- onAnimationEnd();
- mAlphaView.setVisibility(View.INVISIBLE);
- return true;
- }
- else if(MusicInfo.isMusic())
- {
-
- setMusicViewsOnClick();
- }
- break;
-
- default:
- break;
- }
- if(DBG) Log.d(TAG, "onInterceptTouchEvent()");
-
- return false;
- }
-
- @Override
- public boolean onTouchEvent(MotionEvent event) {
-
-
-
-
- if (mTracking)
- {
- final int action = event.getAction();
- final float nx = event.getX();
- final float ny = event.getY();
-
- switch (action) {
- case MotionEvent.ACTION_DOWN:
- showMusicButtons();
- break;
- case MotionEvent.ACTION_MOVE:
- setTargetViewVisible(nx, ny);
-
- handleMoveView(nx, ny);
- break;
- case MotionEvent.ACTION_UP:
- mTracking = false;
- doTriggerEvent(mx, my);
- resetMoveView();
- break;
- case MotionEvent.ACTION_CANCEL:
- mTracking = false;
- doTriggerEvent(mx, my);
- resetMoveView();
- break;
- }
- }
- if(DBG) Log.d(TAG, "onTouchEvent()");
- return mTracking || super.onTouchEvent(event);
- }
-
-
- private void showMusicButtons()
- {
- Intent intent = new Intent(SHOW_MUSIC);
- count ++;
- if(count == 1)
- {
- firstClick = System.currentTimeMillis();
- }
- else if (count == 2)
- {
- secondClick = System.currentTimeMillis();
- if (secondClick - firstClick < 500)
- {
-
-
-
- if(DBG) Log.d(TAG, "count--->" + count);
- if(DBG) Log.d(TAG, "isMusic--->" + MusicInfo.isMusic());
- if (MusicInfo.isMusic())
- {
- setMusicButtonVisibility(false);
- setTargetViewVisible();
- }
- else
- {
- setMusicButtonVisibility(true);
- }
- intent.putExtra("music_show", MusicInfo.isMusic());
- mContext.sendBroadcast(intent);
- }
- count = 0;
- firstClick = 0;
- secondClick = 0;
- }
- }
-
-
- private float dist2(float dx, float dy)
- {
- return dx * dx + dy * dy;
- }
-
- private void handleMoveView(float x, float y)
- {
-
- int mHalfCenterViewWidth = mCenterViewWidth >> 1;
-
-
- int Radius = mCenterViewWidth + mHalfCenterViewWidth;
-
-
-
-
- if (Math.sqrt(dist2(x - mScreenHalfWidth, y - (mCenterView.getTop() + mCenterViewWidth / 2)
- )) > Radius)
- {
-
- x = (float) ((Radius / (Math.sqrt(dist2(x - mScreenHalfWidth, y - (mCenterView.getTop() + mHalfCenterViewWidth)
- )))) * (x - mScreenHalfWidth) + mScreenHalfWidth);
-
- y = (float) ((Radius / (Math.sqrt(dist2(x - mScreenHalfWidth, y - (mCenterView.getTop() + mHalfCenterViewWidth)
- )))) * (y - (mCenterView.getTop() + mHalfCenterViewWidth)) + mCenterView.getTop() + mHalfCenterViewWidth);
- }
-
- mx = x;
- my = y;
-
-
-
-
- mCenterView.setX((int)x - mCenterView.getWidth()/2);
- mCenterView.setY((int)y - mCenterView.getHeight()/2);
- ShowLightView(x, y);
- invalidate();
- }
-
-
- private void doTriggerEvent(float a, float b)
- {
- if (smsRect.contains((int)a, (int) b))
- {
-
- onAnimationEnd();
- setTargetViewInvisible(mSmsView);
- virbate();
-
- mainHandler.obtainMessage(MainActivity.MSG_LAUNCH_SMS).sendToTarget();
- }
- else if (dialRect.contains((int)a , (int)b))
- {
- onAnimationEnd();
- setTargetViewInvisible(mDialView);
- virbate();
- mainHandler.obtainMessage(MainActivity.MSG_LAUNCH_DIAL).sendToTarget();
- }
- else if (cameraRect.contains((int)a, (int)b))
- {
- onAnimationEnd();
- setTargetViewInvisible(mCameraView);
- virbate();
- mainHandler.obtainMessage(MainActivity.MSG_LAUNCH_CAMERA).sendToTarget();
- }
- else if (unlockRect.contains((int)a, (int)b))
- {
- onAnimationEnd();
- setTargetViewInvisible(mUnLockView);
- virbate();
- mainHandler.obtainMessage(MainActivity.MSG_LAUNCH_HOME).sendToTarget();
- }
- }
-
-
- private void ShowLightView(float a, float b)
- {
- if (unlockRect.contains((int)a, (int)b))
- {
- setLightVisible(mUnLockLightView);
- }
- else if (smsRect.contains((int)a, (int) b))
- {
- setLightVisible(mSmsLightView);
- }
- else if (dialRect.contains((int)a , (int)b))
- {
- setLightVisible(mDialLightView);
- }
- else if (cameraRect.contains((int)a, (int)b))
- {
- setLightVisible(mCameraLightView);
- }
- else
- {
- setLightInvisible();
- }
- }
-
- private void setLightVisible(ImageView view)
- {
- view.setVisibility(View.VISIBLE);
- mCenterView.setVisibility(View.INVISIBLE);
- }
-
-
- private void setLightInvisible()
- {
- final View mActivatedViews[] = {mUnLockLightView, mSmsLightView,
- mDialLightView, mCameraLightView};
- for (View view : mActivatedViews)
- {
- view.setVisibility(View.INVISIBLE);
- }
-
- mCenterView.setVisibility(View.VISIBLE);
- }
-
- private void setTargetViewInvisible(ImageView img)
- {
- img.setVisibility(View.INVISIBLE);
- }
-
- private void setTargetViewVisible(float x, float y)
- {
- if(Math.sqrt(dist2(x - mScreenHalfWidth, y - (mCenterView.getTop() + mCenterViewWidth / 2)
- )) > mAlphaViewHeight / 4)
- {
- if (MusicInfo.isMusic())
- {
- setMusicButtonVisibility(false);
- }
- else
- {
- return;
- }
- }
- }
-
- private void setTargetViewVisible()
- {
- if(DBG) Log.d(TAG, "setTargetViewVisible()");
- final View mTargetViews[] = {mSmsView, mDialView, mUnLockView, mCameraView};
- for (View view : mTargetViews)
- {
- view.setVisibility(View.VISIBLE);
- }
- MusicInfo.setMusic(false);
- }
-
-
- private void setMusicButtonVisibility(boolean show)
- {
- int visibility = show ? View.VISIBLE : View.INVISIBLE;
- if(DBG) Log.d(TAG, "setMusicIconVisible()");
- final View mMusicViews[] = {mPlayView, mNextView, mPrevView, mStopView};
- for(View view : mMusicViews)
- {
- view.setVisibility(visibility);
- }
-
-
- if (show)
- {
-
- MusicInfo.setMusic(show);
- }
-
- }
-
-
- private void resetMoveView()
- {
- mCenterView.setX(mWidth / 2 - mCenterViewWidth /2);
- mCenterView.setY((mCenterView.getTop() + mCenterViewHeight / 2) - mCenterViewHeight / 2);
- onAnimationStart();
- if (MusicInfo.isMusic())
- {
- setMusicButtonVisibility(true);
- }
- invalidate();
- }
-
- public void setMainHandler(Handler handler)
- {
- mainHandler = handler;
- }
-
-
- private void virbate()
- {
- Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
- vibrator.vibrate(200);
- }
-
-
- private void setMusicViewsOnClick()
- {
- final View mMusicViews[] = {mPlayView, mNextView, mPrevView, mStopView};
- for (View view : mMusicViews)
- {
- view.setOnClickListener(this);
- }
- }
-
- private void setViewId()
- {
- if(DBG) Log.d(TAG, "setViewId()");
- mPlayView.setId(0);
- mNextView.setId(1);
- mPrevView.setId(2);
- mStopView.setId(3);
- }
-
-
- @Override
- public void onClick(View v)
- {
-
-
- Intent intent = new Intent();
- intent.setAction(SERVICECMD);
-
-
- if(DBG) Log.d(TAG, "onClick");
-
-
-
-
- switch (v.getId())
- {
- case 0:
-
- MainActivity.mStatusViewManager.connectMediaService();
- if (MusicInfo.isPlaying())
- {
-
- intent.putExtra(CMDNAME, CMDPAUSE);
- mPlayView.setImageResource(R.drawable.play);
- }
- else
- {
-
- intent.putExtra(CMDNAME, CMDPLAY);
- mPlayView.setImageResource(R.drawable.pause);
- }
-
- break;
-
- case 1:
- MainActivity.mStatusViewManager.connectMediaService();
-
- intent.putExtra(CMDNAME, CMDNEXT);
- mPlayView.setImageResource(R.drawable.pause);
- break;
-
- case 2:
- MainActivity.mStatusViewManager.connectMediaService();
-
- intent.putExtra(CMDNAME, CMDPREV);
- mPlayView.setImageResource(R.drawable.pause);
- break;
-
- case 3:
-
- MainActivity.mStatusViewManager.connectMediaService();
- intent.putExtra(CMDNAME, CMDSTOP);
- mPlayView.setImageResource(R.drawable.play);
- break;
- }
- mContext.sendBroadcast(intent);
- }
-
- private void setPlayViewDrawable()
- {
- if(DBG) Log.d(TAG, "setPlayViewDrawable()-->" + MusicInfo.isPlaying());
- mPlayView.setImageResource(MusicInfo.isPlaying() ? R.drawable.pause : R.drawable.play);
- }
-
-
- @Override
- protected void onAnimationEnd() {
-
- super.onAnimationEnd();
- if (alpha != null)
- {
- alpha = null;
- }
- mAlphaView.setAnimation(null);
- }
-
-
- @Override
- protected void onAnimationStart() {
-
- super.onAnimationStart();
- mAlphaView.setVisibility(View.VISIBLE);
-
- if (alpha == null) {
- alpha = new AlphaAnimation(0.0f, 1.0f);
- alpha.setDuration(1000);
- }
- alpha.setRepeatCount(Animation.INFINITE);
- mAlphaView.startAnimation(alpha);
- }
-
- }<span style="font-family: 'Comic Sans MS';"> </span>
3. MediaControl.java:
自定义接口,StarLockService和StatusViewManager均实现它,代码如下:
- package com.phicomm.hu;
-
- public interface MediaControl
- {
- void connectMediaService();
- void registerComponent();
- void unregisterComponent();
- void initViews();
- }
4.MainActivity.java:
继承Activity类,承载锁屏整个界面,覆盖手机home界面,作为第三方锁屏的可视化操作界面;主要负责加载界面布局、处理来自StarLockView发送的解锁、启动相关应用的信息、屏蔽相关的物理按钮;代码如下:
- package com.phicomm.hu;
- import com.phicomm.hu.R;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.Window;
- import android.view.WindowManager;
-
- public class MainActivity extends Activity
- {
- private static final boolean DBG = true;
- private static final String TAG = "MainActivity";
- public static final int MSG_LAUNCH_HOME = 0;
- public static final int MSG_LAUNCH_DIAL = 1;
- public static final int MSG_LAUNCH_SMS = 2;
- public static final int MSG_LAUNCH_CAMERA = 3;
-
- private StarLockView mLockView;
- public static StatusViewManager mStatusViewManager;
-
-
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- if(DBG) Log.d(TAG, "onCreate()");
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
- setContentView(R.layout.main);
-
- initViews();
- mStatusViewManager = new StatusViewManager(this, this.getApplicationContext());
-
- mStatusViewManager.connectMediaService();
- startService(new Intent(MainActivity.this, StarLockService.class));
- mLockView.setMainHandler(mHandler);
- }
-
-
- private Handler mHandler = new Handler()
- {
-
- @Override
- public void handleMessage(Message msg)
- {
-
- switch (msg.what)
- {
- case MSG_LAUNCH_HOME:
- finish();
- break;
- case MSG_LAUNCH_SMS:
- launchSms();
- finish();
- break;
- case MSG_LAUNCH_DIAL:
- launchDial();
- finish();
- break;
- case MSG_LAUNCH_CAMERA:
- launchCamera();
- finish();
- break;
- }
- }
-
- };
-
-
- private void launchSms() {
-
-
- Intent intent = new Intent();
- ComponentName comp = new ComponentName("com.android.mms",
- "com.android.mms.ui.ConversationList");
- intent.setComponent(comp);
- intent.setAction("android.intent.action.VIEW");
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
- | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
- startActivity(intent);
- }
-
-
- private void launchDial() {
- Intent intent = new Intent(Intent.ACTION_DIAL);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
- | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
- startActivity(intent);
- }
-
-
- private void launchCamera() {
- Intent intent = new Intent();
- ComponentName comp = new ComponentName("com.android.camera",
- "com.android.camera.Camera");
- intent.setComponent(comp);
- intent.setAction("android.intent.action.VIEW");
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
- | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
- startActivity(intent);
- }
-
-
- @Override
- public void onAttachedToWindow() {
-
- this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
- super.onAttachedToWindow();
- }
-
-
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
-
- return disableKeycode(keyCode, event);
- }
-
- private boolean disableKeycode(int keyCode, KeyEvent event)
- {
- int key = event.getKeyCode();
- switch (key)
- {
- case KeyEvent.KEYCODE_BACK:
- case KeyEvent.KEYCODE_VOLUME_DOWN:
- case KeyEvent.KEYCODE_VOLUME_UP:
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
-
- @Override
- protected void onDestroy() {
-
- super.onDestroy();
- if(DBG) Log.d(TAG, "onDestroy()");
-
- }
-
- @Override
- protected void onResume()
- {
-
- super.onResume();
- if(DBG) Log.d(TAG, "onResume()");
- }
-
- @Override
- public void onDetachedFromWindow()
- {
-
- super.onDetachedFromWindow();
- if(DBG) Log.d(TAG, "onDetachedFromWindow()");
-
- mStatusViewManager.unregisterComponent();
- }
-
- public void initViews()
- {
-
- mLockView = (StarLockView) findViewById(R.id.FxView);
- }
-
- }
注:若4.0以上的系统,launchCamera()方法中的代码改为
- private void launchCamera() {
- Intent intent = new Intent();
- intent.setAction(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
- startActivity(intent);
- }
5. StarLockService.java:
锁屏后台服务类,主要负责动态监听接收按Power键的广播和音乐后台服务的广播;代码实现如下:
- package com.phicomm.hu;
-
- import android.app.KeyguardManager;
- import android.app.Service;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.os.IBinder;
- import android.util.Log;
-
- public class StarLockService extends Service implements MediaControl
- {
-
- private static final boolean DBG = true;
- private static final String TAG = "FxLockService";
- private Intent mFxLockIntent = null;
- private KeyguardManager mKeyguardManager = null ;
- private KeyguardManager.KeyguardLock mKeyguardLock = null ;
-
- private MusicBroadcastReceiver mMBR;
-
- @Override
- public void onCreate()
- {
-
- super.onCreate();
- refreshInfo();
- if (DBG)Log.d(TAG, "-->onCreate()");
- mFxLockIntent = new Intent(StarLockService.this, MainActivity.class);
- mFxLockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- registerComponent();
- }
-
-
-
- @Override
- public void onStart(Intent intent, int startId)
- {
-
- super.onStart(intent, startId);
- if(DBG) Log.d(TAG, "-->onStart()");
- }
-
-
-
- @Override
- public void onDestroy()
- {
-
- super.onDestroy();
- if(DBG) Log.d(TAG, "-->onDestroy()");
- unregisterComponent();
-
- startService(new Intent(StarLockService.this, StarLockService.class));
- }
-
- @Override
- public IBinder onBind(Intent arg0)
- {
-
- if(DBG) Log.d(TAG, "-->onBind()");
- return null;
- }
-
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
-
- if(DBG) Log.d(TAG, "-->onStartCommand()");
- return Service.START_STICKY;
- }
-
-
- private BroadcastReceiver mScreenOnOrOffReceiver = new BroadcastReceiver() {
-
- @Override
- public void onReceive(Context context, Intent intent)
- {
- if(DBG) Log.d(TAG, "mScreenOffReceiver-->" + intent.getAction());
-
- if (intent.getAction().equals("android.intent.action.SCREEN_ON")
- || intent.getAction().equals("android.intent.action.SCREEN_OFF"))
- {
- refreshInfo();
- mKeyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
- mKeyguardLock = mKeyguardManager.newKeyguardLock("FxLock");
-
- mKeyguardLock.disableKeyguard();
-
- startActivity(mFxLockIntent);
- }
-
- if(intent.getAction().equals("com.phicomm.hu.action.music"))
- {
- refreshInfo();
- }
- }
- };
-
-
- private class MusicBroadcastReceiver extends BroadcastReceiver
- {
-
- @Override
- public void onReceive(Context context, Intent intent)
- {
-
- if(DBG) Log.d(TAG, "MusicBroadcastReceiver-->" + intent.getAction());
-
- MusicInfo.setArtistName(intent.getStringExtra("artist"));
-
- MusicInfo.setMusicName(intent.getStringExtra("track"));
-
- MusicInfo.setPlaying(intent.getBooleanExtra("playing", false));
-
-
- setMusicViewText();
-
- if(DBG) Log.d(TAG, "artistName-->" + MusicInfo.getArtistName());
- if(DBG) Log.d(TAG, "musicName-->" + MusicInfo.getMusicName());
- if(DBG) Log.d(TAG, "playing-->" + MusicInfo.isPlaying());
-
-
-
- removeStickyBroadcast(intent);
- }
-
- }
-
-
- private void refreshInfo()
- {
- if (MusicInfo.isMusic())
- {
- setMusicViewText();
- }
- else
- {
- setLockViewText();
- }
- }
-
-
- private void setMusicViewText()
- {
- if(MusicInfo.getArtistName() != null
- || MusicInfo.getMusicName()!= null)
- {
-
-
-
- MainActivity.mStatusViewManager.mArtistView.setText(MusicInfo.getArtistName());
- MainActivity.mStatusViewManager.mMusicView.setText(MusicInfo.getMusicName());
- }
- else
- {
- MainActivity.mStatusViewManager.mArtistView.setText("no music to be chosen");
- }
-
- }
-
-
- private void setLockViewText()
- {
- MainActivity.mStatusViewManager.mArtistView.setText(null);
- }
-
- @Override
- public void connectMediaService()
- {
-
-
- }
-
-
- @Override
- public void registerComponent()
- {
-
- if(DBG) Log.d(TAG, "registerComponent()");
- IntentFilter mScreenOnOrOffFilter = new IntentFilter();
- mScreenOnOrOffFilter.addAction("android.intent.action.SCREEN_ON");
- mScreenOnOrOffFilter.addAction("android.intent.action.SCREEN_OFF");
- mScreenOnOrOffFilter.addAction(StarLockView.SHOW_MUSIC);
- StarLockService.this.registerReceiver(mScreenOnOrOffReceiver, mScreenOnOrOffFilter);
-
- if (mMBR == null)
- {
- mMBR = new MusicBroadcastReceiver();
- IntentFilter mFilter = new IntentFilter();
- mFilter.addAction("com.android.music.playstatechanged");
- mFilter.addAction("com.android.music.metachanged");
- mFilter.addAction("com.android.music.queuechanged");
- mFilter.addAction("com.android.music.playbackcomplete");
- StarLockService.this.registerReceiver(mMBR, mFilter);
- }
- }
-
-
- @Override
- public void unregisterComponent()
- {
-
- if(DBG) Log.d(TAG, "unregisterComponent()");
- if (mScreenOnOrOffReceiver != null)
- {
- StarLockService.this.unregisterReceiver(mScreenOnOrOffReceiver);
- }
-
- if (mMBR != null)
- {
- StarLockService.this.unregisterReceiver(mMBR);
- }
- }
-
- @Override
- public void initViews() {
-
-
- }
-
- }
6. StatusViewManage.java:
该类主要用来管理在锁屏界面上显示时间(包括12小时制下显示上午和下午)和日期。代码如下:
- package com.phicomm.hu;
-
- import java.lang.ref.WeakReference;
- import java.text.DateFormatSymbols;
- import java.util.Calendar;
- import java.util.Date;
-
- import com.phicomm.hu.R;
-
- import android.app.Activity;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.database.ContentObserver;
- import android.graphics.Typeface;
- import android.os.Handler;
- import android.provider.Settings;
- import android.text.format.DateFormat;
- import android.util.Log;
- import android.view.View;
- import android.widget.TextView;
-
-
- public class StatusViewManager implements MediaControl
- {
-
- private static final String SYSTEM = "/system/fonts/";
- private static final String SYSTEM_FONT_TIME_BACKGROUND = SYSTEM + "AndroidClock.ttf";
- private static final String SYSTEM_FONT_TIME_FOREGROUND = SYSTEM + "AndroidClock_Highlight.ttf";
-
- private final static String M12 = "h:mm";
- private final static String M24 = "kk:mm";
-
- private TextView mDateView;
- private TextView mTimeView;
- public TextView mArtistView;
- public TextView mMusicView;
-
- private String mDateFormat;
- private String mFormat;
-
- private static Activity mActivity;
- private AmPm mAmPm;
- private Calendar mCalendar;
- public ContentObserver mFormatChangeObserver;
- public BroadcastReceiver mIntentReceiver;
-
- private final Handler mHandler = new Handler();
-
- private static final Typeface sBackgroundFont;
- private static final Typeface sForegroundFont;
-
- private static Context mContext;
-
- static
- {
-
- sBackgroundFont = Typeface.createFromFile(SYSTEM_FONT_TIME_BACKGROUND);
- sForegroundFont = Typeface.createFromFile(SYSTEM_FONT_TIME_FOREGROUND);
- }
-
- public StatusViewManager(Activity activity, Context context)
- {
- mContext = context;
- mActivity = activity;
- initViews();
- refreshDate();
- }
-
- private View findViewById(int id)
- {
- return mActivity.findViewById(id);
- }
-
- private void refreshDate()
- {
- if (mDateView != null)
- {
-
- mDateView.setText(DateFormat.format(mDateFormat, new Date()));
- }
- }
-
- private String getString(int id)
- {
- return mActivity.getString(id);
- }
-
- class AmPm {
- private TextView mAmPmTextView;
- private String mAmString, mPmString;
-
- AmPm(Typeface tf) {
- mAmPmTextView = (TextView)findViewById(R.id.am_pm);
- if (mAmPmTextView != null && tf != null) {
-
- mAmPmTextView.setTypeface(tf);
- }
-
-
- String[] ampm = new DateFormatSymbols().getAmPmStrings();
- mAmString = ampm[0];
- mPmString = ampm[1];
- }
-
- void setShowAmPm(boolean show) {
- if (mAmPmTextView != null) {
- mAmPmTextView.setVisibility(show ? View.VISIBLE : View.GONE);
- }
- }
-
- void setIsMorning(boolean isMorning) {
- if (mAmPmTextView != null) {
- mAmPmTextView.setText(isMorning ? mAmString : mPmString);
- }
- }
- }
-
- private static class TimeChangedReceiver extends BroadcastReceiver {
- private WeakReference<StatusViewManager> mStatusViewManager;
-
-
- public TimeChangedReceiver(StatusViewManager status) {
- mStatusViewManager = new WeakReference<StatusViewManager>(status);
- }
-
- @Override
- public void onReceive(Context context, Intent intent) {
-
- final boolean timezoneChanged =
- intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED);
- final StatusViewManager status = mStatusViewManager.get();
- if (status != null) {
- status.mHandler.post(new Runnable() {
- public void run() {
- if (timezoneChanged) {
- status.mCalendar = Calendar.getInstance();
- }
- status.updateTime();
- }
- });
- } else {
- try {
- mContext.unregisterReceiver(this);
- } catch (RuntimeException e) {
-
- }
- }
- }
- };
-
-
-
-
- private static class FormatChangeObserver extends ContentObserver {
- private WeakReference<StatusViewManager> mStatusViewManager;
-
- public FormatChangeObserver(StatusViewManager status) {
- super(new Handler());
-
- mStatusViewManager = new WeakReference<StatusViewManager>(status);
- }
- @Override
- public void onChange(boolean selfChange) {
- StatusViewManager mStatusManager = mStatusViewManager.get();
- if (mStatusManager != null) {
- mStatusManager.setDateFormat();
- mStatusManager.updateTime();
- } else {
- try {
- mContext.getContentResolver().unregisterContentObserver(this);
- } catch (RuntimeException e) {
-
- }
- }
- }
- }
-
-
- private void updateTime()
- {
- mCalendar.setTimeInMillis(System.currentTimeMillis());
-
- CharSequence newTime = DateFormat.format(mFormat, mCalendar);
- mTimeView.setText(newTime);
- mAmPm.setIsMorning(mCalendar.get(Calendar.AM_PM) == 0);
- }
-
-
- private void setDateFormat()
- {
- mFormat = android.text.format.DateFormat.is24HourFormat(mContext)
- ? M24 : M12;
- mAmPm.setShowAmPm(mFormat.equals(M12));
- }
-
-
-
-
-
-
-
- @Override
- public void connectMediaService()
- {
-
-
-
-
-
- }
-
- @Override
- public void registerComponent()
- {
-
- Log.d("MainActivity", "registerComponent()");
-
- if (mIntentReceiver == null) {
- mIntentReceiver = new TimeChangedReceiver(this);
- IntentFilter filter = new IntentFilter();
- filter.addAction(Intent.ACTION_TIME_TICK);
- filter.addAction(Intent.ACTION_TIME_CHANGED);
- filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
- mContext.registerReceiver(mIntentReceiver, filter);
- }
-
- if (mFormatChangeObserver == null) {
- mFormatChangeObserver = new FormatChangeObserver(this);
- mContext.getContentResolver().registerContentObserver(
- Settings.System.CONTENT_URI, true, mFormatChangeObserver);
- }
-
- updateTime();
- }
-
- @Override
- public void unregisterComponent()
- {
-
- Log.d("MainActivity", "unregisterComponent()");
- if (mIntentReceiver != null) {
- mContext.unregisterReceiver(mIntentReceiver);
- }
- if (mFormatChangeObserver != null) {
- mContext.getContentResolver().unregisterContentObserver(
- mFormatChangeObserver);
- }
- mFormatChangeObserver = null;
- mIntentReceiver = null;
- }
-
- @Override
- public void initViews()
- {
-
- mDateView = (TextView)findViewById(R.id.date);
-
- mDateFormat = getString(R.string.month_day_year);
- mTimeView = (TextView) findViewById(R.id.time);
- mTimeView.setTypeface(sBackgroundFont);
- mArtistView = (TextView)findViewById(R.id.artist);
- mMusicView = (TextView)findViewById(R.id.music);
-
-
- mArtistView.setSelected(true);
- mMusicView.setSelected(true);
-
-
-
-
- mAmPm = new AmPm(null);
-
- mCalendar = Calendar.getInstance();
-
- setDateFormat();
-
- registerComponent();
- }
-
- }
7. MusicInfo.java:
该类用于更新和保存来自音乐应用的音乐播放状态、歌曲名、歌手等相关的音乐信息。同时向外部提供保存的相关音乐信息;(类似JavaBean) 。代码如下:
- package com.phicomm.hu;
-
- public class MusicInfo
- {
-
- private static boolean isMusic;
- private static boolean playing;
- private static String artistName;
- private static String musicName;
- public static boolean isMusic()
- {
- return isMusic;
- }
- public static void setMusic(boolean isMusic)
- {
- MusicInfo.isMusic = isMusic;
- }
- public static boolean isPlaying()
- {
- return playing;
- }
- public static void setPlaying(boolean playing)
- {
- MusicInfo.playing = playing;
- }
- public static String getArtistName()
- {
- return artistName;
- }
- public static void setArtistName(String artistName)
- {
- MusicInfo.artistName = artistName;
- }
- public static String getMusicName()
- {
- return musicName;
- }
- public static void setMusicName(String musicName)
- {
- MusicInfo.musicName = musicName;
- }
-
- }
关于Android第三方锁屏开发的介绍就到此结束了,呵呵,代码着实有点多,大家想吐槽的尽管槽吧。
最后,请注意以下几点事项:
1. 该锁屏通过控制系统音乐模块后台服务MediaPlaybackService来实现音乐播放的控制,但是由于系统音乐模块后台服务默认是禁止跨进程启动它的,所以本锁屏是无法跨进去启动该服务的;
3. 不过,如果自己的手机有root权限,可以去音乐模块的AndroidManifest.xml文件里修改服务类的exported属性,然后编译音乐模块再push入手机,这样就可以做到控制系统音乐应用模块的后天服务音乐播放控制了。
4. 当然也可以自己编写一个类似音乐模块中的后台服务类控制来控制手机中的音频播放