在不同的生命周期调用不用的WakeLock函数可以使系统正常运行(如果不调用WakeLock.release,则屏幕会一直常亮)

首先添加权限:

1
<
uses-permission
android:name
=
"android.permission.WAKE_LOCK"
></
uses-permission
>

实现代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public
class
test
extends
Activity
 
{
 
    
PowerManager powerManager =
null
;
 
    
WakeLock wakeLock =
null
;
 
    
@Override
 
    
protected
void
onCreate(Bundle savedInstanceState) {
 
        
super
.onCreate(savedInstanceState);
 
        
this
.setContentView(R.layout.main);
 
         
 
        
this
.powerManager = (PowerManager)
this
.getSystemService(Context.POWER_SERVICE);
 
        
this
.wakeLock =
this
.powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK,
"My Lock"
);
 
    
}
 
    
@Override
 
    
protected
void
onResume() {
 
        
super
.onResume();
 
        
this
.wakeLock.acquire();
 
    
}
 
    
@Override
 
    
protected
void
onPause() {
 
        
super
.onPause();
 
        
this
.wakeLock.release();
 
    
}
 
}