// 启动设备管理(隐式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;
}
}
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;
}
}
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;
}
}
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;
}
}
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黑白名单的各种黑操作之类的。