Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Trên Android 11 (SDK cấp 30) trở lên, các ứng dụng có thể dùng ý định android.provider.Settings.ACTION_WIFI_ADD_NETWORKS để hướng dẫn người dùng thêm một hoặc nhiều mạng đã lưu mới hoặc cấu hình Passpoint. API này cũng hoạt động như bình thường để sửa đổi các cấu hình đã lưu hiện có.
Để lưu một mạng hoặc cấu hình Passpoint, hãy làm như sau:
Tạo một ý định ACTION_WIFI_ADD_NETWORKS.
Tạo một hoặc nhiều cấu hình bằng cách sử dụng WifiNetworkSuggestion.Builder.
Xin lưu ý rằng mặc dù bạn sử dụng WifiNetworkSuggestion, nhưng Intent API này không liên quan đến Suggestion API.
Tạo một danh sách mảng có thể phân chia của các cấu hình và đính kèm danh sách đó vào ý định bằng phần bổ sung EXTRA_WIFI_NETWORK_LIST.
Nếu resultCode là RESULT_OK, thì dữ liệu Intent chứa EXTRA_WIFI_NETWORK_RESULT_LIST bổ sung, chứa một mảng mã kết quả cho biết từng cấu hình đã được lưu thành công hay chưa. Các mã kết quả có thể là:
Nếu yêu cầu thành công, nền tảng sẽ kích hoạt một kết nối đến một trong các mạng mới lưu.
Mã mẫu
Mẫu mã sau đây cho biết cách lưu cấu hình mạng hoặc Passpoint.
classMainActivity:AppCompatActivity(){overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState)...}funstartOperation(){valsuggestions=ArrayList<WifiNetworkSuggestion>()// WPA2 configurationsuggestions.add(WifiNetworkSuggestion.Builder().setSsid("test111111").setWpa2Passphrase("test123456").build())// Open configurationsuggestions.add(WifiNetworkSuggestion.Builder().setSsid("test222222").build())// Passpoint configurationvalconfig=PasspointConfiguration()config.credential=Credential().apply{realm="realm.example.com"simCredential=Credential.SimCredential().apply{eapType=18imsi="123456*"}}config.homeSp=HomeSp().apply{fqdn="test1.example.com"friendlyName="Some Friendly Name"}suggestions.add(WifiNetworkSuggestion.Builder().setPasspointConfig(config).build())// Create intentvalbundle=Bundle()bundle.putParcelableArrayList(EXTRA_WIFI_NETWORK_LIST,suggestions)valintent=Intent(ACTION_WIFI_ADD_NETWORKS)intent.putExtras(bundle)// Launch intentstartActivityForResult(intent,0)}overridefunonActivityResult(requestCode:Int,resultCode:Int,data:Intent?){super.onActivityResult(requestCode,resultCode,data)if(resultCode==RESULT_OK){// user agreed to save configurations: still need to check individual resultsif(data!=null && data.hasExtra(EXTRA_WIFI_NETWORK_RESULT_LIST)){for(codeindata.getIntegerArrayListExtra(EXTRA_WIFI_NETWORK_RESULT_LIST)){when(code){ADD_WIFI_RESULT_SUCCESS->
...// Configuration saved or modifiedADD_WIFI_RESULT_ADD_OR_UPDATE_FAILED->
...// Something went wrong - invalid configurationADD_WIFI_RESULT_ALREADY_EXISTS->
...// Configuration existed (as-is) on device, nothing changedelse->
...// Other errors}}}}else{// User refused to save configurations}}}
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-08-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-27 UTC."],[],[],null,["On Android 11 (SDK level 30) and higher, apps can use the\n[`android.provider.Settings.ACTION_WIFI_ADD_NETWORKS`](/reference/android/provider/Settings#ACTION_WIFI_ADD_NETWORKS)\nintent to guide the user through adding one or more new saved networks or\nPasspoint configurations. The API also works as-is to modify existing saved\nconfigurations.\n| **Note:** This API is the closest in functionality to the deprecated `WifiManager.addNetwork(WifiConfiguration config)` API, in that the resulting configuration is added to the user-facing and managed saved network and subscription list.\n\nTo save a network or Passpoint configuration, do the following:\n\n1. Create an `ACTION_WIFI_ADD_NETWORKS` intent.\n\n2. Create one or more configurations using\n [`WifiNetworkSuggestion.Builder`](/reference/android/net/wifi/WifiNetworkSuggestion.Builder).\n Note that even though you use a `WifiNetworkSuggestion`, this Intent API is\n not related to the [Suggestion API](/develop/connectivity/wifi-suggest).\n\n3. Create a parcelable array list of the configurations and attach it to the\n intent with the\n [`EXTRA_WIFI_NETWORK_LIST`](/reference/android/provider/Settings#EXTRA_WIFI_NETWORK_LIST)\n extra.\n\n4. Execute\n [`Activity.startActivityForResult()`](/reference/android/app/Activity#startActivityForResult(android.content.Intent,%20int)),\n passing in the intent.\n\n5. Listen for the result using the\n [`Activity.onActivityResult()`](/reference/android/app/Activity#onActivityResult(int,%20int,%20android.content.Intent))\n callback.\n\n The `resultCode` can be one of the following:\n - [`Activity.RESULT_OK`](/reference/android/app/Activity#RESULT_OK): indicating that the user accepted the proposed networks and saved them.\n - [`Activity.RESULT_CANCELED`](/reference/android/app/Activity#RESULT_CANCELED): indicating that the user rejected the proposed networks.\n\n If the `resultCode` is `RESULT_OK`, then the data `Intent` contains the\n [`EXTRA_WIFI_NETWORK_RESULT_LIST`](/reference/android/provider/Settings#EXTRA_WIFI_NETWORK_RESULT_LIST)\n extra, which contains an array of result codes indicating whether individual\n configurations were saved successfully. The possible result codes are:\n - [`ADD_WIFI_RESULT_SUCCESS`](/reference/android/provider/Settings#ADD_WIFI_RESULT_SUCCESS): configuration added or successfully updated.\n - [`ADD_WIFI_RESULT_ADD_OR_UPDATE_FAILED`](/reference/android/provider/Settings#ADD_WIFI_RESULT_ADD_OR_UPDATE_FAILED): failure when trying to add configuration, such as due to a badly formed configuration.\n - [`ADD_WIFI_RESULT_ALREADY_EXISTS`](/reference/android/provider/Settings#ADD_WIFI_RESULT_ALREADY_EXISTS): the requested configuration already existed so no action was necessary.\n6. If the request is successful, the platform triggers a connection to one of the\n newly saved networks.\n\nCode sample\n\nThe following code sample shows how to save a network or Passpoint\nconfiguration. \n\n class MainActivity : AppCompatActivity() {\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n ...\n }\n\n fun startOperation() {\n val suggestions = ArrayList\u003cWifiNetworkSuggestion\u003e()\n\n // WPA2 configuration\n suggestions.add(\n WifiNetworkSuggestion.Builder()\n .setSsid(\"test111111\")\n .setWpa2Passphrase(\"test123456\")\n .build()\n )\n\n // Open configuration\n suggestions.add(\n WifiNetworkSuggestion.Builder()\n .setSsid(\"test222222\")\n .build()\n )\n\n // Passpoint configuration\n val config = PasspointConfiguration()\n config.credential = Credential().apply {\n realm = \"realm.example.com\"\n simCredential = Credential.SimCredential().apply {\n eapType = 18\n imsi = \"123456*\"\n }\n }\n config.homeSp = HomeSp().apply {\n fqdn = \"test1.example.com\"\n friendlyName = \"Some Friendly Name\"\n }\n suggestions.add(\n WifiNetworkSuggestion.Builder()\n .setPasspointConfig(config)\n .build())\n\n // Create intent\n val bundle = Bundle()\n bundle.putParcelableArrayList(EXTRA_WIFI_NETWORK_LIST, suggestions)\n val intent = Intent(ACTION_WIFI_ADD_NETWORKS)\n intent.putExtras(bundle)\n\n // Launch intent\n startActivityForResult(intent, 0)\n }\n\n override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {\n super.onActivityResult(requestCode, resultCode, data)\n if(resultCode == RESULT_OK) {\n // user agreed to save configurations: still need to check individual results\n if (data != null && data.hasExtra(EXTRA_WIFI_NETWORK_RESULT_LIST)) {\n for (code in data.getIntegerArrayListExtra(EXTRA_WIFI_NETWORK_RESULT_LIST)) {\n when (code) {\n ADD_WIFI_RESULT_SUCCESS -\u003e\n ... // Configuration saved or modified\n ADD_WIFI_RESULT_ADD_OR_UPDATE_FAILED -\u003e\n ... // Something went wrong - invalid configuration\n ADD_WIFI_RESULT_ALREADY_EXISTS -\u003e\n ... // Configuration existed (as-is) on device, nothing changed\n else -\u003e\n ... // Other errors\n }\n }\n }\n } else {\n // User refused to save configurations\n }\n }\n }"]]