1e41f4b71Sopenharmony_ci# @system.network (Network State) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci> **NOTE** 4e41f4b71Sopenharmony_ci> - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version. 5e41f4b71Sopenharmony_ci> 6e41f4b71Sopenharmony_ci> - The APIs of this module are no longer maintained since API version 8. You are advised to use ['@ohos.net.connection'](js-apis-net-connection.md). 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci## Modules to Import 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci``` 12e41f4b71Sopenharmony_ciimport network from '@system.network'; 13e41f4b71Sopenharmony_ci``` 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci## Required Permissions 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ciohos.permission.GET_WIFI_INFO 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ciohos.permission.GET_NETWORK_INFO 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci## network.getType<sup>3+</sup> 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_cigetType(options?: {<br> 26e41f4b71Sopenharmony_ci success?: (data: NetworkResponse) => void;<br> 27e41f4b71Sopenharmony_ci fail?: (data: any, code: number) => void;<br> 28e41f4b71Sopenharmony_ci complete?: () => void;<br> 29e41f4b71Sopenharmony_ci}): void 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ciObtains the network type of this device. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci**Parameters** 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 38e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 39e41f4b71Sopenharmony_ci| success | Function | No| Called when the API call is successful. The return value is defined by [NetworkResponse](#networkresponse3).| 40e41f4b71Sopenharmony_ci| fail | Function | No| Called when an API call fails.| 41e41f4b71Sopenharmony_ci| complete | Function | No| Called when an API call is complete.| 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ciError codes: 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci| Error Code| Description| 46e41f4b71Sopenharmony_ci| -------- | -------- | 47e41f4b71Sopenharmony_ci| 602 | The current permission is not declared.| 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci**Example** 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci``` 52e41f4b71Sopenharmony_ciexport default class Network { 53e41f4b71Sopenharmony_ci getType() { 54e41f4b71Sopenharmony_ci network.getType({ 55e41f4b71Sopenharmony_ci success: (data) => { 56e41f4b71Sopenharmony_ci console.log('success get network type:' + data.type); 57e41f4b71Sopenharmony_ci } 58e41f4b71Sopenharmony_ci }); 59e41f4b71Sopenharmony_ci } 60e41f4b71Sopenharmony_ci} 61e41f4b71Sopenharmony_ci``` 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci 64e41f4b71Sopenharmony_ci## network.subscribe<sup>3+</sup> 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_cisubscribe(options?:{<br> 67e41f4b71Sopenharmony_ci success?: (data: NetworkResponse) => void;<br> 68e41f4b71Sopenharmony_ci fail?: (data: any, code: number) => void;<br> 69e41f4b71Sopenharmony_ci }): void 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ciListens to the network connection state of this device. If this API is called multiple times, the last call takes effect. 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 74e41f4b71Sopenharmony_ci 75e41f4b71Sopenharmony_ci**Parameters** 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 78e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 79e41f4b71Sopenharmony_ci| success | Function | No| Called when the network state changes. The return value is defined by [NetworkResponse](#networkresponse3).| 80e41f4b71Sopenharmony_ci| fail | Function | No| Called when an API call fails.| 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ciError codes: 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci| Error Code| Description| 85e41f4b71Sopenharmony_ci| -------- | -------- | 86e41f4b71Sopenharmony_ci| 602 | The current permission is not declared.| 87e41f4b71Sopenharmony_ci| 200 | Subscription failed.| 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci**Example** 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci``` 92e41f4b71Sopenharmony_ciexport default class Network { 93e41f4b71Sopenharmony_ci subscribe() { 94e41f4b71Sopenharmony_ci network.subscribe({ 95e41f4b71Sopenharmony_ci success: (data) => { 96e41f4b71Sopenharmony_ci console.log('success get network type:' + data.type); 97e41f4b71Sopenharmony_ci } 98e41f4b71Sopenharmony_ci }); 99e41f4b71Sopenharmony_ci } 100e41f4b71Sopenharmony_ci} 101e41f4b71Sopenharmony_ci``` 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci## network.unsubscribe<sup>3+</sup> 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ciunsubscribe(): void 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ciCancels listening to the network connection state of this device. 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci**Example** 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci``` 115e41f4b71Sopenharmony_ciimport network from '@system.network'; 116e41f4b71Sopenharmony_ci 117e41f4b71Sopenharmony_cinetwork.unsubscribe(); 118e41f4b71Sopenharmony_ci``` 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci## NetworkResponse<sup>3+</sup> 122e41f4b71Sopenharmony_ci 123e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.NetManager.Core 124e41f4b71Sopenharmony_ci 125e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 126e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 127e41f4b71Sopenharmony_ci| metered | boolean | No|Whether to charge by traffic.| 128e41f4b71Sopenharmony_ci| type | string | Yes|Network type. The value can be **2G**, **3G**, **4G**, **5G**, **WiFi**, or **none**.| 129