Memantau status konektivitas dan pengukuran koneksi
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
ConnectivityManager
menyediakan
API yang memungkinkan Anda meminta perangkat terhubung ke jaringan berdasarkan
berbagai kondisi yang mencakup kemampuan perangkat dan opsi transportasi data.
Implementasi callback memberikan informasi ke aplikasi Anda tentang status koneksi perangkat serta kemampuan jaringan yang terhubung saat ini. API ini memungkinkan Anda menentukan apakah perangkat saat ini terhubung ke jaringan yang memenuhi persyaratan aplikasi Anda.
Untuk menentukan jenis transportasi jaringan, seperti koneksi Wi-Fi atau seluler, dan kemampuan jaringan yang saat ini terhubung, seperti koneksi internet, Anda harus mengonfigurasi permintaan jaringan.
Deklarasikan NetworkRequest
yang
menjelaskan kebutuhan koneksi jaringan aplikasi Anda. Kode berikut membuat
permintaan untuk jaringan yang terhubung ke internet dan menggunakan koneksi Wi-Fi
atau seluler untuk jenis transport.
Kotlin
val networkRequest = NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.build()
Java
NetworkRequest networkRequest = new NetworkRequest.Builder()
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.build();
Perhatikan bahwa beberapa koneksi bisa jauh lebih mahal daripada yang lain (misalnya, koneksi seluler biasanya mahal). Gunakan
NetworkCapabilities#NET_CAPABILITY_NOT_METERED
untuk menentukan apakah koneksi mahal. Saat menggunakan koneksi berbayar, coba kurangi penggunaan data aplikasi, atau tunda hingga perangkat memiliki koneksi tidak berbayar.
Saat mendaftarkan NetworkRequest
dengan ConnectivityManager
, Anda harus menerapkan
NetworkCallback
untuk menerima notifikasi tentang perubahan status koneksi dan kemampuan
jaringan.
Fungsi yang paling umum diterapkan di NetworkCallback
mencakup hal berikut:
onAvailable()
menunjukkan bahwa perangkat terhubung ke jaringan baru yang memenuhi
persyaratan kemampuan dan jenis transportasi yang ditentukan dalam NetworkRequest
.
onLost()
menunjukkan bahwa perangkat telah kehilangan koneksi ke jaringan.
onCapabilitiesChanged()
menunjukkan bahwa kemampuan jaringan telah berubah. Objek
NetworkCapabilities
memberikan informasi tentang kemampuan jaringan saat ini.
Kotlin
private val networkCallback = object : ConnectivityManager.NetworkCallback() {
// network is available for use
override fun onAvailable(network: Network) {
super.onAvailable(network)
}
// Network capabilities have changed for the network
override fun onCapabilitiesChanged(
network: Network,
networkCapabilities: NetworkCapabilities
) {
super.onCapabilitiesChanged(network, networkCapabilities)
val unmetered = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)
}
// lost network connection
override fun onLost(network: Network) {
super.onLost(network)
}
}
Java
private ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(@NonNull Network network) {
super.onAvailable(network);
}
@Override
public void onLost(@NonNull Network network) {
super.onLost(network);
}
@Override
public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
super.onCapabilitiesChanged(network, networkCapabilities);
final boolean unmetered = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
}
};
Mendaftar untuk mendapatkan info terbaru jaringan
Setelah Anda mendeklarasikan NetworkRequest
dan NetworkCallback
, gunakan fungsi
requestNetwork()
atau registerNetworkCallback()
untuk menelusuri jaringan yang akan disambungkan dari perangkat yang memenuhi
NetworkRequest
. Status kemudian dilaporkan ke NetworkCallback
.
Kotlin
val connectivityManager = getSystemService(ConnectivityManager::class.java) as ConnectivityManager
connectivityManager.requestNetwork(networkRequest, networkCallback)
Java
ConnectivityManager connectivityManager =
(ConnectivityManager) getSystemService(ConnectivityManager.class);
connectivityManager.requestNetwork(networkRequest, networkCallback);
Konten dan contoh kode di halaman ini tunduk kepada lisensi yang dijelaskan dalam Lisensi Konten. Java dan OpenJDK adalah merek dagang atau merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-08-27 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-08-27 UTC."],[],[],null,["The [`ConnectivityManager`](/reference/android/net/ConnectivityManager) provides\nan API that enables you to request that the device connect to a network based on\nvarious conditions that include device capabilities and data transport options.\n\nThe callback implementation provides information to your app about the device's\nconnection status as well as the capabilities of the currently connected\nnetwork. The API enables you to determine whether the device is currently\nconnected to a network that satisfies your app's requirements.\n\nConfigure a network request\n\nTo specify the transport type of the network, such as Wi-Fi or cellular\nconnection, and the currently connected network's capabilities, such as internet\nconnection, you must configure a network request.\n\nDeclare a [`NetworkRequest`](/reference/android/net/NetworkRequest) that\ndescribes your app's network connection needs. The following code creates a\nrequest for a network that is connected to the internet and uses either a Wi-Fi\nor cellular connection for the transport type. \n\nKotlin \n\n```kotlin\nval networkRequest = NetworkRequest.Builder()\n .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)\n .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)\n .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)\n .build()\n```\n\nJava \n\n```java\nNetworkRequest networkRequest = new NetworkRequest.Builder()\n .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)\n .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)\n .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)\n .build();\n```\n\nNote that some connections can be significantly more expensive than others (for\nexample, a mobile connection is typically expensive). Use\n[`NetworkCapabilities#NET_CAPABILITY_NOT_METERED`](/reference/android/net/NetworkCapabilities#NET_CAPABILITY_NOT_METERED)\nto determine whether the connection is expensive. When on a metered connection,\ntry to reduce your app's data consumption, or delay it until the device has a\nnon-metered connection.\n\nConfigure a network callback\n\nWhen you register the `NetworkRequest` with the `ConnectivityManager`, you must\nimplement a\n[`NetworkCallback`](/reference/android/net/ConnectivityManager.NetworkCallback)\nto receive notifications about changes in the connection status and network\ncapabilities.\n\nThe most commonly implemented functions in the `NetworkCallback` include the\nfollowing:\n\n- [`onAvailable()`](/reference/android/net/ConnectivityManager.NetworkCallback#onAvailable(android.net.Network)) indicates that the device is connected to a new network that satisfies the capabilities and transport type requirements specified in the `NetworkRequest`.\n- [`onLost()`](/reference/android/net/ConnectivityManager.NetworkCallback#onLost(android.net.Network)) indicates that the device has lost connection to the network.\n- [`onCapabilitiesChanged()`](/reference/android/net/ConnectivityManager.NetworkCallback#onCapabilitiesChanged(android.net.Network,%20android.net.NetworkCapabilities)) indicates that the capabilities of the network have changed. The [`NetworkCapabilities`](/reference/android/net/NetworkCapabilities) object provides information about the current capabilities of the network.\n\nKotlin \n\n```kotlin\nprivate val networkCallback = object : ConnectivityManager.NetworkCallback() {\n // network is available for use\n override fun onAvailable(network: Network) {\n super.onAvailable(network)\n }\n\n // Network capabilities have changed for the network\n override fun onCapabilitiesChanged(\n network: Network,\n networkCapabilities: NetworkCapabilities\n ) {\n super.onCapabilitiesChanged(network, networkCapabilities)\n val unmetered = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)\n }\n\n // lost network connection\n override fun onLost(network: Network) {\n super.onLost(network)\n }\n}\n```\n\nJava \n\n```java\nprivate ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {\n @Override\n public void onAvailable(@NonNull Network network) {\n super.onAvailable(network);\n }\n\n @Override\n public void onLost(@NonNull Network network) {\n super.onLost(network);\n }\n\n @Override\n public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {\n super.onCapabilitiesChanged(network, networkCapabilities);\n final boolean unmetered = networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);\n }\n};\n```\n\nRegister for network updates\n\nAfter you declare the `NetworkRequest` and `NetworkCallback`, use the\n[`requestNetwork()`](/reference/android/net/ConnectivityManager#requestNetwork(android.net.NetworkRequest,%20android.net.ConnectivityManager.NetworkCallback))\nor [`registerNetworkCallback()`](/reference/android/net/ConnectivityManager#registerNetworkCallback(android.net.NetworkRequest,%20android.net.ConnectivityManager.NetworkCallback))\nfunctions to search for a network to connect from the device that satisfies the\n`NetworkRequest`. The status is then reported to the `NetworkCallback`. \n\nKotlin \n\n```kotlin\nval connectivityManager = getSystemService(ConnectivityManager::class.java) as ConnectivityManager\nconnectivityManager.requestNetwork(networkRequest, networkCallback)\n```\n\nJava \n\n```java\nConnectivityManager connectivityManager =\n (ConnectivityManager) getSystemService(ConnectivityManager.class);\nconnectivityManager.requestNetwork(networkRequest, networkCallback);\n```"]]