本文实例讲述了android编程获取系统隐藏服务实现锁屏的方法。分享给大家供大家参考,具体如下:
实现原理:当按锁屏键时,会发出一个广播,当界面接收到一个广播就可以实现锁频。我们可以调用idevicepolicymanager服务中的locknow方法来发送一个广播实现锁屏。
idevicepolicymanager是被系统隐藏掉的,需要通过反射还获取此服务。
步骤:
1.创建myadmin的广播接收者继承deviceadminreceiver
2.通过反射 ,获取idevicepolicymanager服务 ,idevicepolicymanager通过aidl来获取出来。
3.注册广播接收者为admin设备
4.获取服务中的方法
效果图:
注册myadmin广播接收者:
1
2
3
4
5
6
7
|
<receiver android:name= ".myadmin" >
<meta-data android:name= "android.app.device_admin"
android:resource= "@xml/my_admin" />
<intent-filter>
<action android:name= "android.app.action.device_admin_enabled" />
</intent-filter>
</receiver>
|
my_admin.xml:
1
2
3
4
5
6
7
8
9
10
|
<?xml version= "1.0" encoding= "utf-8" ?>
<device-admin xmlns:android= "http://schemas.android.com/apk/res/android" >
<uses-policies>
<limit-password />
<watch-login />
<reset-password />
<force-lock />
<wipe-data />
</uses-policies>
</device-admin>
|
反射获取服务、注册权限、实现锁屏:
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
|
public class lockactivity extends activity {
idevicepolicymanager mservice;
@override
public void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.main);
}
//锁屏
public void lock(view view){
try {
//通过反射获取到sdk隐藏的服务
method method = class .forname( "android.os.servicemanager" )
.getmethod( "getservice" , string. class );
ibinder binder = (ibinder) method.invoke( null , //激活服务
new object[] { context.device_policy_service });
mservice = idevicepolicymanager.stub.asinterface(binder);
//定义组件的名字
componentname madminname = new componentname( this , myadmin. class );
//注册权限
if (mservice != null ) {
//判断自定义的广播接受者 是不是被注册成 deviceadmin的权限
if (!mservice.isadminactive(madminname)) {
intent intent = new intent(
devicepolicymanager.action_add_device_admin);
intent.putextra(devicepolicymanager.extra_device_admin,
madminname);
startactivity(intent);
}
//调用服务实现锁屏
mservice.locknow();
//设置解锁密码
mservice.resetpassword( "123" , 0 );
}
} catch (exception e) {
e.printstacktrace();
}
}
}
|
aidl:
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
|
/*
**
** copyright 2010, the android open source project
**
** licensed under the apache license, version 2.0 (the "license");
** you may not use this file except in compliance with the license.
** you may obtain a copy of the license at
**
** http://www.apache.org/licenses/license-2.0
**
** unless required by applicable law or agreed to in writing, software
** distributed under the license is distributed on an "as is" basis,
** without warranties or conditions of any kind, either express or implied.
** see the license for the specific language governing permissions and
** limitations under the license.
*/
package android.app.admin;
import android.content.componentname;
/**
* internal ipc interface to the device policy service.
* {@hide}
*/
interface idevicepolicymanager {
void setpasswordquality(in componentname who, int quality);
int getpasswordquality(in componentname who);
void setpasswordminimumlength(in componentname who, int length);
int getpasswordminimumlength(in componentname who);
boolean isactivepasswordsufficient();
int getcurrentfailedpasswordattempts();
void setmaximumfailedpasswordsforwipe(in componentname admin, int num);
int getmaximumfailedpasswordsforwipe(in componentname admin);
boolean resetpassword(string password, int flags);
void setmaximumtimetolock(in componentname who, long timems);
long getmaximumtimetolock(in componentname who);
void locknow();
void wipedata( int flags);
void setactiveadmin(in componentname policyreceiver);
boolean isadminactive(in componentname policyreceiver);
list<componentname> getactiveadmins();
boolean packagehasactiveadmins(string packagename);
void removeactiveadmin(in componentname policyreceiver);
void setactivepasswordstate( int quality, int length);
void reportfailedpasswordattempt();
void reportsuccessfulpasswordattempt();
}
|
希望本文所述对大家android程序设计有所帮助。