Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Unter Android 11 (SDK-Ebene 30) und höher können Apps den Intent android.provider.Settings.ACTION_WIFI_ADD_NETWORKS verwenden, um den Nutzer beim Hinzufügen eines oder mehrerer neuer gespeicherter Netzwerke oder Passpoint-Konfigurationen zu unterstützen. Die API kann auch unverändert verwendet werden, um vorhandene gespeicherte Konfigurationen zu ändern.
So speichern Sie eine Netzwerk- oder Passpoint-Konfiguration:
Erstellen Sie einen ACTION_WIFI_ADD_NETWORKS-Intent.
Erstellen Sie mit WifiNetworkSuggestion.Builder eine oder mehrere Konfigurationen.
Auch wenn Sie eine WifiNetworkSuggestion verwenden, hat diese Intent API nichts mit der Suggestion API zu tun.
Erstellen Sie eine Parcelable-Array-Liste der Konfigurationen und hängen Sie sie mit dem Extra EXTRA_WIFI_NETWORK_LIST an den Intent an.
Wenn resultCode den Wert RESULT_OK hat, enthält Intent das zusätzliche EXTRA_WIFI_NETWORK_RESULT_LIST, das ein Array von Ergebniscodes enthält, die angeben, ob einzelne Konfigurationen erfolgreich gespeichert wurden. Mögliche Ergebniscodes:
ADD_WIFI_RESULT_ALREADY_EXISTS:
Die angeforderte Konfiguration war bereits vorhanden, daher waren keine Maßnahmen erforderlich.
Wenn die Anfrage erfolgreich ist, stellt die Plattform eine Verbindung zu einem der neu gespeicherten Netzwerke her.
Codebeispiel
Das folgende Codebeispiel zeigt, wie Sie eine Netzwerk- oder Passpoint-Konfiguration speichern.
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}}}
Alle Inhalte und Codebeispiele auf dieser Seite unterliegen den Lizenzen wie im Abschnitt Inhaltslizenz beschrieben. Java und OpenJDK sind Marken oder eingetragene Marken von Oracle und/oder seinen Tochtergesellschaften.
Zuletzt aktualisiert: 2025-08-27 (UTC).
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Benötigte Informationen nicht gefunden","missingTheInformationINeed","thumb-down"],["Zu umständlich/zu viele Schritte","tooComplicatedTooManySteps","thumb-down"],["Nicht mehr aktuell","outOfDate","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Problem mit Beispielen/Code","samplesCodeIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 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 }"]]