每次我们锁键盘的时候,都看到如下页面 :
看多了就产生审美疲劳 , 手机是追求个性化的消费品,想到就是替换她,再找一个。
如何替换呢? Android 提供如下函数 :
KeyguardManager mKeyguardManager = (KeyguardManager)getSystemService(Context. KEYGUARD_SERVICE );
KeyguardLock mKeyguardLock = mKeyguardManager.newKeyguardLock( "" );
mKeyguardLock.disableKeyguard();
拿到键盘守护锁,屏蔽她既可。接下来就是在合适的时机替代她 , 这个合适的时机就是接收 Intent.ACTION_SCREEN_OFF.
IntentFilter filter = new IntentFilter(Intent. ACTION_SCREEN_OFF );
registerReceiver( mReceiver , filter);
这里有点奇怪的是,如果通过 android.manifest 配置 receiver 的话,就得不到 Action_screen_off 事件,我现在还纳闷了 ……
最后就是再找一个她 ( 个性化屏保 ) 了 , 我目前在模仿 Hero 屏保,所以就以她来说了。
首先用一个服务启动一个 Activity.
如下:
Intent it = new Intent();
it.setClass( this , ScreenShow. class );
it.setFlags(Intent. FLAG_ACTIVITY_NEW_TASK );
this .startActivity(it);
还要设置该 Activity 为单态,并且为透明 , 属性如下 :
android:launchMode="singleTask" android:theme="@style/Theme.nd_Dialog" 其中透明 Activity 的 Style 设置为
< style name = "Theme.nd_Dialog" parent = "android:style/Theme.Translucent" >
< item name = "android:windowNoTitle" > true </ item >
</ style >
我们体验过 Hero 屏保界面,就会发现,屏保的移动与手势的用力大小相关。用力重的时候,屏保界面自动往下滑。
VelocityTracker mVelocityTracker;
if ( mVelocityTracker == null ) {
mVelocityTracker = VelocityTracker.obtain ();
}
mVelocityTracker.addMovement(event);
mVelocityTracker .computeCurrentVelocity (1000);
float yVel = mVelocityTracker .getYVelocity();
其中屏保自动往下滑的动作不是用 Animation 来做的,而是通过 Handler, 隔一段时间发送一个消息达到动画的效果 :
mHandler .sendMessageAtTime( mHandler .obtainMessage( MSG_ANIMATE ),
mCurAnimationTime );
比起 Animation, 通过 Handle 可以达到自如地控制动画 .
最后,讲讲屏保内容用到 Receiver ,
// 时间 Receiver
intentFilter.addAction(Intent. ACTION_TIME_CHANGED );
intentFilter.addAction(Intent. ACTION_TIME_TICK );
intentFilter.addAction(Intent. ACTION_TIMEZONE_CHANGED );
// 配置变化 , 关闭系统 ,
intentFilter.addAction(Intent. ACTION_CONFIGURATION_CHANGED );
intentFilter.addAction(Intent. ACTION_CLOSE_SYSTEM_DIALOGS );
// 网络名称 Receiver( 比如中国移动等 ) intentFilter.addAction(Telephony .Intents.SPN_STRINGS_UPDATED_ACTION);
// 电量改变 Receiver
intentFilter.addAction(Intent. ACTION_BATTERY_CHANGED );
// Sim 状态 Receiver intentFilter.addAction(TelephonyIntents .ACTION_SIM_STATE_CHANGED;