-----------关于android屏幕保护---屏幕常亮---------屏幕解锁----很不错的文章.

文章1:

android 屏幕保持唤醒 不锁屏 android.permission.WAKE_LOCK

http://blog.csdn.net/laoyang1018/article/details/8000264

n AndroidManifest.xml 加上权限:
<uses-permission android:name="android.permission.WAKE_LOCK" />


方法一:
public class UnLockActivity2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

}


方法二:
public class UnLockActivity extends Activity {
/** Called when the activity is first created. */


WakeLock m_wklk;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
m_wklk = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "cn");


m_wklk.acquire(); //设置保持唤醒
}


@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
m_wklk.release(); //解除保持唤醒
}


@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();


m_wklk.release();//解除保持唤醒


}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();




m_wklk.acquire(); //设置保持唤醒
}
}


解释:


用到的类


PowerManager
主要是这两个参数:PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE


下面是 android 官方API 解释:
he following flags are defined, with varying effects on system power.These flags are mutually exclusive - you may only specify one of them.


Flag Value CPUScreen Keyboard
PARTIAL_WAKE_LOCK On*Off Off
SCREEN_DIM_WAKE_LOCK OnDim Off
SCREEN_BRIGHT_WAKE_LOCK OnBright Off
FULL_WAKE_LOCK OnBright Bright
一般要使程序运行过程中背景保持常亮,使用


SCREEN_BRIGHT_WAKE_LOCK 就可以,


SCREEN_BRIGHT_WAKE_LOCK CPU:唤醒 屏幕背光:唤醒 键盘灯:关闭


第二个参数:


In addition, you can add two more flags, which affect behavior of the screen only.These flags have no effect when combined with aPARTIAL_WAKE_LOCK.


Flag Value Description
ACQUIRE_CAUSES_WAKEUP Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.
ON_AFTER_RELEASE If this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to remain on a bit longer. This can be used to reduce flicker if you are cycling between wake lock conditions.


文章二:Android下屏保程序的开发--须知

http://blog.csdn.net/wh_19910525/article/details/8544682

1、注册一个BroadcastReceiver,用于接收ACTION_SCREEN_OFF事件,当接收到这个事件时,打开自定义的屏保画面。
Intent.ACTION_SCREEN_OFF.
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(mReceiver, filter);
BroadcastReceiver mMasterResetReciever= new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent){
        try{
            Intent i = new Intent();
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//解释
            i.setClass(context, ScreenSaverActivity.class);
            context.startActivity(i);
        }catch(Exception e){
            Log.i("Output:", e.toString());
        }
    }
};
2、完成这一步时,虽然屏幕关闭时,屏保画面被显示,但由于接收的是ACTION_SCREEN_OFF事件,所以屏幕已经变黑了,这个时候就需要强制点亮屏幕。
首先,在AndroidManifest.xml中加上permission 权限:
<uses-permission android:name="android.permission.WAKE_LOCK" />
以下代码在ScreenSaverActivity.onCreate中,
//点亮 屏幕
PowerManager pm = (PowerManager)getSystemService(POWER_SERVICE);
mWakeLock = pm.newWakeLock(
    PowerManager.ACQUIRE_CAUSES_WAKEUP |
    PowerManager.SCREEN_DIM_WAKE_LOCK |
    PowerManager.ON_AFTER_RELEASE, "SimpleTimer");
在ScreenSaverActivity.onResume中加入
mWakeLock.acquire();

为了能够在退出屏保时,能够让屏幕可以关闭,我们必须释放WakeLock.故在ScreenSaverActivity.onPause中加入
mWakeLock.acquire();
3. 完成第二步后,一般情况下大功告成。按键就可以返回程序。但有时候需要点击屏幕就返回系统。这个时候会发现,无论怎么样点击屏幕,系统都不会有响应,因为 此时屏幕已经 被锁定。因此,要实现这个功能,在打开屏保时,需要关闭屏幕的锁定功能。
首先,在AndroidManifest.xml中加上permission 权限:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

把以下这段代码加入合适的地方。只要启动屏保时能够被调用到
// 拿到键盘守护锁,解锁屏幕
KeyguardManager mKeyguardManager = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock mKeyguardLock = mKeyguardManager.newKeyguardLock("");
mKeyguardLock.disableKeyguard();//解锁屏幕,也就是 关闭 屏幕 锁定 功能

当然也可以通过以下代码开启屏幕锁定功能。
mKeyguardLock.reenableKeyguard();
+++++++++++++++++++ 具体实现自定义锁屏控制系统的大致思想 +++++++++++++++++++++

(1)在主界面中设计一个锁屏控制按钮,决定是否进入 自定义的 锁屏 控制界面; 

(2) 需要设计一个Service,在启动时,即在 它的 onCreate方法中注册监听系统屏幕锁屏的广播行为ACTION_SCREEN_OFF: "android.intent.action.SCREEN_OFF";
(3)在上述serice中定义一个广播接收器:如果监听到 ACTION_SCREEN_OFF,则进入到自定义的锁屏控制界面,即启动自定义实现的Activity;  
---------------难点实现:
  (1)ACTION_SCREEN_OFF该消息需要动态注册,不能在XML文件中全局注册 
  (2)收到screen_off消息后,如何让自己定义的锁屏界面在系统锁屏界面之上 
  (3)点亮屏幕时,如何防止启动自定义锁屏界面时出现的闪屏现象 
  (4)在某些机型某些时候需要点亮两次屏幕 
  (5)解自定义锁屏时需要先解系统锁 
   (6)如何做到在解系统锁时在多机型多Rom下的兼容 
   (7)如何做到在解系统锁后不对耳机线控等行为或者其他相关行为产生影响 
   (8)如何在解系统锁后,要及时的能恢复系统锁 
    (9)有其他第三方锁屏系统存在时,如何保证自己的锁屏在其他所有锁屏之上。 
   (10)如何在锁屏界面屏蔽相关按键,特别是屏蔽Home键,特别是4.0系统之上 
---------------------------   
 5、小结 
        简单的实现自定义锁屏,我想大部分人在很短的时间内都能实现,关键是要整合到第三应用中要保证其能和大多数机型和Rom能兼容,也就是要解决上面写到的难点问题。
----------------------------屏幕变亮 和 变暗 发出的broadcast是这个:

android.intent.action.SCREEN_ON和android.intent.action.SCREEN_OFF

对于SCRESCREEN_ON/SCREEN_OFF,不可以到Manifest里面注册,要在代码里面用RegisterReceive()注册才有效。

////======================================================================================================================
public static void wakeUpAndUnlock(Context context){
        KeyguardManager km= (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");
        //解锁
        kl.disableKeyguard();
        //获取电源管理器对象
        PowerManager pm=(PowerManager) context.getSystemService(Context.POWER_SERVICE);
        //获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK,"bright");
        //点亮屏幕
        wl.acquire();
        //释放
        wl.release();
    }










加入权限
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />



你可能感兴趣的:(-----------关于android屏幕保护---屏幕常亮---------屏幕解锁----很不错的文章.)