目录

DeviceVpnManager

Class Info

public class DeviceVpnManager

VPN相关管理类。

Public Method Summary

Qualifier and Type

Method Name and Description

boolean

setVpnDisabled(ComponentName admin, boolean isDisabled)

禁用/启用 VPN。

boolean

isVpnDisabled(ComponentName admin)

查询 VPN 的禁用状态。

boolean

setVpnProfile(ComponentName admin, DeviceVpnProfile profile)

配置VPN。

boolean

deleteVpnProfile(ComponentName admin, String key)

删除VPN。

List<String>

getVpnList(ComponentName admin)

获取VPN 列表。

DeviceVpnProfile

getVpnProfile(ComponentName admin, String key)

根据指定的 key 获取对应的 VPN 文件。

boolean

setInsecureVpnDisabled(ComponentName admin, boolean isDisabled)

设置仅允许使用安全VPN。

boolean

isInsecureVpnDisabled(ComponentName admin)

获取仅允许使用安全VPN的禁用状态。

boolean

setShowVpnPasswordDisabled(ComponentName admin, boolean isDisabled)

禁用/启用VPN 密码编辑框的查看功能。

boolean

isShowVpnPasswordDisabled(ComponentName admin)

查询VPN密码编辑框的查看功能状态。

Public Methods

[h2]setVpnDisabled

Supported Devices

Device Type

OS Version

手机、平板

EMUI 5.1及以上或HarmonyOS 2.0及以上

Method

public boolean setVpnDisabled(ComponentName admin, boolean isDisabled)

设置手机的VPN禁用策略。当禁用手机VPN时,会断掉已经连接的VPN,手机的VPN界面会置灰,不可编辑,同时也不可以通过三方应用连接VPN。当允许使用时,手机的 VPN功能恢复正常。

注意:需要申请com.huawei.permission.sec.MDM_VPN权限。

Parameters

Name

Description

admin

调用该接口的组件名称,不能为null。

isDisabled

  • true:禁用VPN。
  • false:允许使用VPN。

Return

Type

Description

boolean

  • true:配置成功。
  • false:配置失败。

Throws

Name

Description

SecurityException

  • 此APK未经设备管理激活。
  • 无com.huawei.permission.sec.MDM_VPN权限。

IllegalArgumentException

参数错误或admin为null。

[h2]isVpnDisabled

Supported Devices

Device Type

OS Version

手机、平板

EMUI 5.1及以上或HarmonyOS 2.0及以上

Method

public boolean isVpnDisabled(ComponentName admin)

获取手机中的VPN禁用策略。

Parameters

Name

Description

admin

调用该接口的组件名称,查询该应用配置的策略;参数为null时,获取所有应用设置的综合策略结果。

Return

Type

Description

boolean

  • true:VPN被禁用。
  • false:VPN允许使用。

[h2]setVpnProfile

Supported Devices

Device Type

OS Version

手机、平板

EMUI 5.1及以上或HarmonyOS 2.0及以上

Method

public boolean setVpnProfile(ComponentName admin, DeviceVpnProfile profile)

配置手机中的VPN,配置文件profile中有关键字key来区分各个VPN,若key已存在则修改已配置的VPN,若key不存在则增加一个新的VPN。

注意:需要申请com.huawei.permission.sec.MDM_VPN权限。

Parameters

Name

Description

admin

调用该接口的组件名称,不能为null。

profile

存放VPN的配置。

DeviceVpnProfile构造方法

说明

public DeviceVpnProfile(String key)

传入key构造,其余使用默认值,String类型为“”,type为0,mppe 为true。

public DeviceVpnProfile(String key, String name, int type, String server,String username, String password, boolean mppe, String l2tpSecret,String ipsecIdentifier, String ipsecSecret, String ipsecUserCert,String ipsecCaCert, String ipsecServerCert)

传入所有的属性的参数。

属性类型

属性名称

说明

String

key

VPN的唯一标识,不可为null,不可为空字符串。

String

name

VPN的名称。

int

type

VPN 的类型,有效范围 0-6。

  • 0:TYPE_PPTP
  • 1:TYPE_L2TP
  • 2:TYPE_L2TP_IPSEC_PSK
  • 3:TYPE_L2TP_IPSEC_RSA
  • 4:TYPE_IPSEC_XAUTH_PSK
  • 5:TYPE_IPSEC_XAUTH_RSA
  • 6:TYPE_IPSEC_HYBRID_RSA

String

server

服务器名称。

String

username

用户名。

String

password

密码。

boolean

mppe

PPP加密。

String

l2tpSecret

L2TP密钥。

String

ipsecIdentifier

IPSec标识符。

String

ipsecSecret

IPSec预共享密钥。

String

ipsecUserCert

IPSec用户证书。

String

ipsecCaCert

IPSec CA证书.

String

ipsecServerCert

IPSec 服务器证书。

Return

Type

Description

boolean

  • true:配置成功。
  • false:配置失败。

Throws

Name

Description

SecurityException

  • 此APK未经设备管理激活。
  • 无com.huawei.permission.sec.MDM_VPN权限。

IllegalArgumentException

参数错误或admin 为null。

[h2]deleteVpnProfile

Supported Devices

Device Type

OS Version

手机、平板

EMUI 5.1及以上或HarmonyOS 2.0及以上

Method

public boolean deleteVpnProfile(ComponentName admin, String key)

根据传入的key来删除手机中的VPN,若存在该key的VPN则删除,若不存在该VPN则返回失败。

注意:需要申请com.huawei.permission.sec.MDM_VPN权限。

Parameters

Name

Description

admin

调用该接口的组件名称,不能为null。

key

用来区分VPN的标识。

Return

Type

Description

boolean

  • true:配置成功。
  • false:配置失败。

Throws

Name

Description

SecurityException

  • 此APK未经设备管理激活。
  • 无com.huawei.permission.sec.MDM_VPN权限。

IllegalArgumentException

参数错误,或admin为null。

[h2]getVpnList

Supported Devices

Device Type

OS Version

手机、平板

EMUI 5.1及以上或HarmonyOS 2.0及以上

Method

public List<String> getVpnList(ComponentName admin)

获取手机中的所有的 VPN 的 key 组成的列表。

Parameters

Name

Description

admin

调用该接口的应用的组件名称。参数admin若不为空,则获取的是本应用配置的VPN的key组成的列表;若admin为空则获取的是手机中所有应用配置的VPN的key组成的列表。

Return

Type

Description

List<String>

VPN的key组成的列表。若没有配置VPN,则返回一个空表。

[h2]getVpnProfile

Supported Devices

Device Type

OS Version

手机、平板

EMUI 5.1及以上或HarmonyOS 2.0及以上

Method

public DeviceVpnProfile getVpnProfile(ComponentName admin, String key)

根据指定的key获取对应的VPN文件,若不存在则返回null。

Parameters

Name

Description

admin

调用该接口的组件名称,查询该应用配置的策略;参数为null时,获取所有应用设置的综合策略结果。

key

VPN的唯一标识,用来获取VPN配置文件。

Return

Type

Description

DeviceVpnProfile

传入的key所对应的VPN配置文件。

Throws

Name

Description

SecurityException

此APK未经设备管理激活。

[h2]setInsecureVpnDisabled

Supported Devices

Device Type

OS Version

手机、平板

EMUI 8.0及以上或HarmonyOS 2.0及以上

Method

public boolean setInsecureVpnDisabled(ComponentName admin, boolean isDisabled)

设置手机中不安全VPN的禁用策略。

注意:需要申请com.huawei.permission.sec.MDM_VPN权限。

Parameters

Name

Description

admin

调用该接口的组件名称。

isDisabled

  • true:仅允许安全VPN连接。
  • false:允许所有VPN连接。

Return

Type

Description

boolean

  • true:配置成功。
  • false:配置失败。

Throws

Name

Description

SecurityException

  • 此APK未经设备管理激活。
  • 无com.huawei.permission.sec.MDM_VPN权限。

IllegalArgumentException

参数错误,或admin为null。

[h2]isInsecureVpnDisabled

Supported Devices

Device Type

OS Version

手机、平板

EMUI 8.0及以上或HarmonyOS 2.0及以上

Method

public boolean isInsecureVpnDisabled(ComponentName admin)

获取手机中不安全VPN的禁用策略。

Parameters

Name

Description

admin

调用该接口的组件名称,查询该应用配置的策略;参数为null时,获取所有应用设置的综合策略结果。

Return

Type

Description

boolean

  • true:不安全VPN被禁用。
  • false:不安全VPN允许使用。

[h2]setShowVpnPasswordDisabled

Supported Devices

Device Type

OS Version

手机、平板

EMUI 10.1.0及以上或HarmonyOS 2.0及以上

Method

public boolean setShowVpnPasswordDisabled(ComponentName admin, boolean isDisabled)

禁用/启用VPN密码编辑框的查看功能。

注意:需要申请com.huawei.permission.sec.MDM_VPN权限。

Parameters

Name

Description

admin

调用该接口的组件名称。

isDisabled

  • true:禁用VPN密码编辑框的查看功能。
  • false:启用VPN密码编辑框的查看功能。

Return

Type

Description

boolean

  • true:配置成功。
  • false:配置失败。

Throws

Name

Description

SecurityException

  • 此APK未经设备管理激活。
  • 无com.huawei.permission.sec.MDM_VPN权限。

IllegalArgumentException

参数错误,或admin为null。

[h2]isShowVpnPasswordDisabled

Supported Devices

Device Type

OS Version

手机、平板

EMUI 10.1.0及以上或HarmonyOS 2.0及以上

Method

public boolean isShowVpnPasswordDisabled(ComponentName admin)

查询VPN密码编辑框的查看功能是否禁用。

Parameters

Name

Description

admin

调用该接口的组件名称。为null时,获取的是全局综合策略结果。

Return

Type

Description

boolean

  • true:VPN密码编辑框的查看功能被禁用。
  • false:VPN密码编辑框的查看功能未被禁用。