一、激活设备管理器
使用MDM功能要先激活设备管理器
代码如下:
/**
* 激活设备管理权限 成功执行激活时,DeviceAdminReceiver中的 onEnabled 会响应
*/
private void activeManage() {
// 启动设备管理(隐式Intent) - 在AndroidManifest.xml中设定相应过滤器
Intent intents = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
// 权限列表
intents.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
// 描述(additional explanation)
intents.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"------ 其他描述 ------");
startActivityForResult(intents, 0);
}
二、可以远程对设备做哪些操作
要先获得DevicePolicyManager实例才可以进行控制操作
DevicePolicyManager mDPM = (DevicePolicyManager) mContext
.getSystemService(Context.DEVICE_POLICY_SERVICE);
1、控制锁屏
mDPM.lockNow();
2、控制摄像机的使用
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public int setCameraDisabled(boolean disableCamera) {
if (isDeviceAdminActive()) {
if (API_LEVEL < 14) {
return ERROR_UNSUPPORTED;
}
mDPM.setCameraDisabled(mDeviceAdminSample, disableCamera);
return 0;
} else {
//
activateDeviceAdmin();
return ERROR_UNACTIVATED;
}
}
public boolean getCameraDisabled() {
if (isDeviceAdminActive()) {
if (API_LEVEL < 14) {
return false;
}
return mDPM.getCameraDisabled(mDeviceAdminSample);
} else {
//
activateDeviceAdmin();
return false;
}
}
3、控制密码
public int resetPasswd(String newPasswd) {
if (isDeviceAdminActive()) {
boolean succeed = mDPM.resetPassword(newPasswd,
DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
if (succeed) {
return 0;
} else {
return ERROR_PASSWD_QUALITY;
}
} else {
// 激活时间与用户的操作有关,所以不等待,直接返回操作失败
activateDeviceAdmin();
return ERROR_UNACTIVATED;
}
}
public int setPasswdQuality(int value) {
if (isDeviceAdminActive()) {
if (API_LEVEL < 11) {
return ERROR_UNSUPPORTED;
}
mDPM.setPasswordQuality(mDeviceAdminSample, value);
return 0;
} else {
activateDeviceAdmin();
return ERROR_UNACTIVATED;
}
}
public String getPasswordQuality() {
if (isDeviceAdminActive()) {
int quality = mDPM.getPasswordQuality(mDeviceAdminSample);
switch (quality) {
case DevicePolicyManager.PASSWORD_QUALITY_BIOMETRIC_WEAK:
return "图案";
case DevicePolicyManager.PASSWORD_QUALITY_NUMERIC:
return "数字";
case DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC:
return "字母";
case DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC:
return "数字和字母";
default:
return "其它";
}
} else {
activateDeviceAdmin();
return "未知";
}
}
public int setPasswordMinimumLength(int value) {
if (isDeviceAdminActive()) {
mDPM.setPasswordMinimumLength(mDeviceAdminSample, value);
return 0;
} else {
activateDeviceAdmin();
return ERROR_UNACTIVATED;
}
}
/*
* -1无法获取
*/
public int getPasswordMinimumLength() {
if (isDeviceAdminActive()) {
return mDPM.getPasswordMinimumLength(mDeviceAdminSample);
} else {
activateDeviceAdmin();
return -1;
}
}
// TODO: 2017-2-7 策略信息中并没有该项
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public int setPasswordMinimumLetters(int value) {
if (isDeviceAdminActive()) {
if (API_LEVEL < 11) {
return ERROR_UNSUPPORTED;
}
mDPM.setPasswordMinimumLetters(mDeviceAdminSample, value);
return 0;
} else {
activateDeviceAdmin();
return ERROR_UNACTIVATED;
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public int setPasswordMinimumNumeric(int value) {
if (isDeviceAdminActive()) {
if (API_LEVEL < 11) {
return ERROR_UNSUPPORTED;
}
mDPM.setPasswordMinimumNumeric(mDeviceAdminSample, value);
return 0;
} else {
activateDeviceAdmin();
return ERROR_UNACTIVATED;
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public int setPasswordMinimumLowerCase(int value) {
if (isDeviceAdminActive()) {
if (API_LEVEL < 11) {
return ERROR_UNSUPPORTED;
}
mDPM.setPasswordMinimumLowerCase(mDeviceAdminSample, value);
return 0;
} else {
activateDeviceAdmin();
return ERROR_UNACTIVATED;
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public int setPasswordMinimumUpperCase(int value) {
if (isDeviceAdminActive()) {
if (API_LEVEL < 11) {
return ERROR_UNSUPPORTED;
}
mDPM.setPasswordMinimumUpperCase(mDeviceAdminSample, value);
return 0;
} else {
activateDeviceAdmin();
return ERROR_UNACTIVATED;
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public int setPasswordMinimumSymbols(int value) {
if (isDeviceAdminActive()) {
if (API_LEVEL < 11) {
return ERROR_UNSUPPORTED;
}
mDPM.setPasswordMinimumSymbols(mDeviceAdminSample, value);
return 0;
} else {
activateDeviceAdmin();
return ERROR_UNACTIVATED;
}
}
/*
* -1无法获取
*/
public int getPasswordMinimumSymbols() {
if (isDeviceAdminActive()) {
return mDPM.getPasswordMinimumSymbols(mDeviceAdminSample);
} else {
activateDeviceAdmin();
return -1;
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public int setPasswordMinimumNonLetter(int value) {
if (isDeviceAdminActive()) {
if (API_LEVEL < 11) {
return ERROR_UNSUPPORTED;
}
mDPM.setPasswordMinimumNonLetter(mDeviceAdminSample, value);
return 0;
} else {
activateDeviceAdmin();
return ERROR_UNACTIVATED;
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public int setPasswordHistoryLength(int value) {
if (isDeviceAdminActive()) {
if (API_LEVEL < 11) {
return ERROR_UNSUPPORTED;
}
mDPM.setPasswordHistoryLength(mDeviceAdminSample, value);
return 0;
} else {
activateDeviceAdmin();
return ERROR_UNACTIVATED;
}
}
/*
* -1无法获取
*/
public int getPasswordHistoryLength() {
if (isDeviceAdminActive()) {
return mDPM.getPasswordHistoryLength(mDeviceAdminSample);
} else {
activateDeviceAdmin();
return -1;
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public int setPasswordExpirationTimeout(int value) {
if (isDeviceAdminActive()) {
if (API_LEVEL < 11) {
return ERROR_UNSUPPORTED;
}
mDPM.setPasswordExpirationTimeout(mDeviceAdminSample, value
* MS_PER_DAY);
return 0;
} else {
activateDeviceAdmin();
return ERROR_UNACTIVATED;
}
}
/*
* -1无法获取
*/
public long getPasswordExpirationTimeout() {
if (isDeviceAdminActive()) {
long ret = mDPM.getPasswordExpirationTimeout(mDeviceAdminSample);
return ret / MS_PER_DAY;
} else {
activateDeviceAdmin();
return -1;
}
}
4、控制蓝牙
// 打开、关闭蓝牙
public int setBluetoothState(boolean enable) {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter
.getDefaultAdapter();
if (bluetoothAdapter == null) {
return ERROR_UNSUPPORTED;
}
if (enable) {
bluetoothAdapter.enable();
} else {
bluetoothAdapter.disable();
}
return 0;
}
5、WIFI控制相关
public int getWifiState() {
android.net.wifi.WifiManager wifiManager = (android.net.wifi.WifiManager) mContext
.getSystemService(Context.WIFI_SERVICE);
return wifiManager.getWifiState();
}
// 打开、关闭wifi
public void setWifiState(boolean enable) {
android.net.wifi.WifiManager wifiManager = (android.net.wifi.WifiManager) mContext
.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(enable);
}
// 打开、关闭wifi
public void disconnectWifi() {
android.net.wifi.WifiManager wifiManager = (android.net.wifi.WifiManager) mContext
.getSystemService(Context.WIFI_SERVICE);
wifiManager.getConnectionInfo().getNetworkId();
WifiAdmin wifiAdmin = new WifiAdmin(mContext);
wifiAdmin.disconnectWifi(wifiManager.getConnectionInfo().getNetworkId());
//wifiManager.disconnect();
}
6、设置静音
// 设置静音
public int setMute(boolean setMute) {
if (setMute) {
AudioManager audioManager = (AudioManager) mContext
.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0); // mute
// music
// stream
audioManager.setStreamVolume(AudioManager.STREAM_RING, 0, 0); // mute
// ring
// stream
if (Build.VERSION.SDK_INT >= 8) {
audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
audioManager.requestAudioFocus(null, AudioManager.STREAM_RING,
AudioManager.AUDIOFOCUS_GAIN);
}
} else {
AudioManager audioManager = (AudioManager) mContext
.getSystemService(Context.AUDIO_SERVICE);
int maxMusic = audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
audioManager
.setStreamVolume(AudioManager.STREAM_MUSIC, maxMusic, 0); // mute
// music
// stream
int maxRing = audioManager
.getStreamMaxVolume(AudioManager.STREAM_RING);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxRing, 0); // mute
// ring
// stream
if (Build.VERSION.SDK_INT >= 8) {
audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
audioManager.requestAudioFocus(null, AudioManager.STREAM_RING,
AudioManager.AUDIOFOCUS_GAIN);
}
}
return 0;
}
7、恢复出厂设置
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public int wipeData(boolean withExternalStorage) {
if (isDeviceAdminActive()) {
if (API_LEVEL >= 9) {
// mDPM.wipeData(withExternalStorage ? DevicePolicyManager.WIPE_EXTERNAL_STORAGE
// : 0);
mDPM.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);
} else {
// WIPE_EXTERNAL_STORAGE is not supported under API level 9
mDPM.wipeData(0);
}
return 0;
} else {
activateDeviceAdmin();
return ERROR_UNACTIVATED;
}
}
要想实现更多的管理服务得去不同厂家申请了,比如申请加入APP白名单的接口,wifi黑白名单的各种黑操作之类的。
有问题欢迎留言讨论
热文推荐:
1、大厂又有新的开源项目了,赶紧来领取...
2、面试官问我:一个 TCP 连接可以发多少个 HTTP 请求?我竟然回答不上来...
3、程序员疑似出bug被吊打!菲律宾的高薪工作机会了解一下?
4、“一键脱衣”的DeepNude下架后,我在GitHub上找到它涉及的技术
5、原生Android开发的路该怎么走
6、太厉害了,终于有人能把TCP/IP 协议讲的明明白白了
7、腾讯开源超实用的UI轮子库,我是轮子搬运工
8、腾讯新开源一吊炸天神器—零反射全动态Android插件框架正式开源
喜欢 就关注吧,欢迎投稿!