public class ConnectivityReport {
Network network;
long reportTimestamp;
LinkProperties linkProperties;
NetworkCapabilities networkCapabilities;
PersistableBundle additionalInfo;
}
可疑数据失速:系统会怀疑存在数据失速,即 IP 数据包无法正常流经网络的情况。DataStallReport 类可提供有关疑似数据失速的信息。
public class DataStallReport {
Network network;
long reportTimestamp;
int detectionMethod;
LinkProperties linkProperties;
NetworkCapabilities networkCapabilities;
PersistableBundle stallDetails;
}
NetworkRequestrequest=newNetworkRequest.Builder().addTransportType(TRANSPORT_CELLULAR).build();// Use an Executor that is appropriate for your use caseExecutorexecutor=Executors.newSingleThreadExecutor();ConnectivityDiagnosticsManagercdm=context.getSystemService(ConnectivityDiagnosticsManager.class);ExampleCallbackcallback=newExampleCallback();cdm.registerConnectivityDiagnosticsCallback(request,executor,callback);...// Collect connectivity information on networks that match with request...cdm.unregisterConnectivityDiagnosticsCallback(callback);
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-03-26。"],[],[],null,["# Connectivity Diagnostics API\n\nThe Connectivity Diagnostics API allows apps that own or manage networks, such\nas carrier apps, VPN apps, and Wi-Fi suggestion apps, to receive diagnostic\nnetwork connectivity information from the framework. These apps can register\ncallbacks and receive notifications with connectivity information for the\nnetworks that they own or manage. Apps won't receive notifications for networks\nthat aren't owned or managed by the app.\n\nThe following are examples of apps that manage or own networks:\n\n- **Carrier apps:** Manage cellular networks for which their `subId` has carrier privileges for\n- **Wi-Fi suggestion apps:** Own Wi-Fi networks that they suggest to the system\n- **VPN apps:** Manage all networks that their VPN uses, but only when they are the active VPN\n\nCallbacks are invoked in the following cases:\n\n- **Network validation:** The system finished evaluating a specific network.\n The\n [`ConnectivityReport`](https://developer.android.com/reference/android/net/ConnectivityDiagnosticsManager.ConnectivityReport)\n class provides information on the current state of the network and the\n results of any tests or procedures performed as part of the validation.\n\n public class ConnectivityReport {\n Network network;\n long reportTimestamp;\n LinkProperties linkProperties;\n NetworkCapabilities networkCapabilities;\n PersistableBundle additionalInfo;\n }\n\n- **Data stall suspected:** A data stall, a condition in which IP packets\n aren't properly flowing through the network, is suspected. The\n [`DataStallReport`](https://developer.android.com/reference/android/net/ConnectivityDiagnosticsManager.DataStallReport)\n class provides information about suspected data stalls.\n\n public class DataStallReport {\n Network network;\n long reportTimestamp;\n int detectionMethod;\n LinkProperties linkProperties;\n NetworkCapabilities networkCapabilities;\n PersistableBundle stallDetails;\n }\n\n- **Connectivity reported:** An app has reported connectivity through\n [`ConnectivityManager#reportNetworkConnectivity`](https://developer.android.com/reference/android/net/ConnectivityManager#reportNetworkConnectivity(android.net.Network,%20boolean))\n to the system. The network and reported connectivity (whether the app\n believes the network does or doesn't provide connectivity) is shared.\n\nImplementation\n--------------\n\n| **Note:** Apps registering callbacks must have the [`ACCESS_FINE_LOCATION`](https://developer.android.com/reference/android/Manifest.permission#ACCESS_FINE_LOCATION) permission because connecting to location-specific networks can expose the fine location of the user.\n\nTo use the Connectivity Diagnostics API, an app must obtain a\n[`ConnectivityDiagnosticsManager`](https://developer.android.com/reference/android/net/ConnectivityDiagnosticsManager)\ninstance from the platform. This instance should be used to register and\nunregister\n[`ConnectivityDiagnosticsCallback`](https://developer.android.com/reference/android/net/ConnectivityDiagnosticsManager.ConnectivityDiagnosticsCallback)\nimplementations. Callback methods that aren't overridden are no-ops.\n\nBelow is an example of a\n[`ConnectivityDiagnosticsCallback`](https://developer.android.com/reference/android/net/ConnectivityDiagnosticsManager.ConnectivityDiagnosticsCallback)\nimplementation: \n\n public class ExampleCallback extends ConnectivityDiagnosticsCallback {\n @Override\n public void onConnectivityReportAvailable(@NonNull ConnectivityReport report) {\n ... \n // Log data, take action based on report result, etc\n ... \n }\n\n @Override\n public void onDataStallSuspected(@NonNull DataStallReport report) {\n ... \n // Log data, take action based on report result, etc\n ... \n }\n\n @Override\n public void onNetworkConnectivityReported(\n @NonNull Network network, boolean hasConnectivity) {\n ... \n // Log data, take action based on report result, etc\n ... \n }\n }\n\nTo register callbacks and receive notifications, call\n[`registerConnectivityDiagnosticsCallback`](https://developer.android.com/reference/android/net/ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback(android.net.NetworkRequest,%20java.util.concurrent.Executor,%20android.net.ConnectivityDiagnosticsManager.ConnectivityDiagnosticsCallback)).\nTo unregister callbacks and stop receiving notifications, call\n[`unregisterConnectivityDiagnosticsCallback`](https://developer.android.com/reference/android/net/ConnectivityDiagnosticsManager#unregisterConnectivityDiagnosticsCallback(android.net.ConnectivityDiagnosticsManager.ConnectivityDiagnosticsCallback)).\n| **Note:** The callbacks must be registered and unregistered using the same [`uid`](https://developer.android.com/reference/android/content/pm/ApplicationInfo.html#uid).\n\nBelow is an example for registering and unregistering\n`ConnectivityDiagnosticsCallback`: \n\n NetworkRequest request =\n new NetworkRequest.Builder()\n .addTransportType(TRANSPORT_CELLULAR)\n .build();\n // Use an Executor that is appropriate for your use case\n Executor executor = Executors.newSingleThreadExecutor();\n\n ConnectivityDiagnosticsManager cdm =\n context.getSystemService(ConnectivityDiagnosticsManager.class);\n\n ExampleCallback callback = new ExampleCallback();\n cdm.registerConnectivityDiagnosticsCallback(\n request, executor, callback);\n\n ... \n // Collect connectivity information on networks that match with request\n ... \n\n cdm.unregisterConnectivityDiagnosticsCallback(callback);\n\nValidation\n----------\n\nThe Connectivity Diagnostics API is CTS tested by\n[`ConnectivityDiagnosticsManagerTest`](https://cs.android.com/android/platform/superproject/+/android-latest-release:packages/modules/Connectivity/tests/cts/net/src/android/net/cts/ConnectivityDiagnosticsManagerTest.java)."]]