Menentukan dan memantau jenis dan status terpasang ke dok
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Perangkat Android dapat dipasang ke berbagai jenis dok. Ini
termasuk dok mobil, dok rumah, dan dok digital atau analog. Status doknya adalah
biasanya terkait erat dengan status pengisian daya, karena banyak dok menyediakan daya untuk
perangkat yang dipasang ke dok.
Aplikasi Anda memengaruhi kecepatan update ponsel saat berada di dok. Anda dapat meningkatkan
frekuensi pembaruan aplikasi berita olahraga saat berada di dok desktop, atau
menonaktifkan pembaruan sepenuhnya jika perangkat dipasang ke dok di mobil. Sebaliknya, Anda dapat
memaksimalkan update saat dipasang ke dok mobil jika layanan latar belakang Anda diupdate
kondisi lalu lintas.
Status dok juga disiarkan sebagai sticky
Intent
,
memungkinkan Anda untuk menanyakan apakah perangkat dipasang ke dok dan di dok jenis apa
begitu.
Menentukan status terpasang ke dok saat ini
Detail status terpasang ke dok disertakan sebagai tambahan dalam
siaran melekat
ACTION_DOCK_EVENT
tindakan. Karena melekat, Anda dapat memanggil
registerReceiver()
,
meneruskan null
sebagai penerima siaran. Cuplikan berikut
menunjukkan cara menyelesaikan proses ini:
Kotlin
val dockStatus: Intent? = IntentFilter(Intent.ACTION_DOCK_EVENT).let { ifilter ->
context.registerReceiver(null, ifilter)
}
Java
IntentFilter ifilter = new IntentFilter(Intent.ACTION_DOCK_EVENT);
Intent dockStatus = context.registerReceiver(null, ifilter);
Anda bisa mengekstrak status terpasang ke dok saat ini dari
Tambahan EXTRA_DOCK_STATE
:
Kotlin
val dockState: Int = dockStatus?.getIntExtra(EXTRA_DOCK_STATE, -1) ?: -1
val isDocked: Boolean = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED
Java
int dockState -1;
if (dockStatus != null) {
dockState = dockStatus.getIntExtra(EXTRA_DOCK_STATE, -1);
}
boolean isDocked = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;
Menentukan jenis dok saat ini
Jika perangkat dipasang ke dok, perangkat dapat dipasang ke salah satu dari empat
berbagai jenis dok:
- Car
- Meja
- Desktop Virtual (Analog)
- Meja Kelas Atas (Digital)
Dua opsi terakhir hanya diperkenalkan di Android API level 11, jadi
praktik yang baik untuk memeriksa ketiga jenis desktop tersebut
jenis dok, bukan dalam bentuk digital atau analog:
Kotlin
val isCar: Boolean = dockState == EXTRA_DOCK_STATE_CAR
val isDesk: Boolean = dockState == EXTRA_DOCK_STATE_DESK
|| dockState == EXTRA_DOCK_STATE_LE_DESK
|| dockState == EXTRA_DOCK_STATE_HE_DESK
Java
boolean isCar = dockState == EXTRA_DOCK_STATE_CAR;
boolean isDesk = dockState == EXTRA_DOCK_STATE_DESK ||
dockState == EXTRA_DOCK_STATE_LE_DESK ||
dockState == EXTRA_DOCK_STATE_HE_DESK;
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-07-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-07-27 UTC."],[],[],null,["# Determine and monitor the docking state and type\n\nAndroid-powered devices can be docked into different kinds of docks. These\ninclude car docks, home docks, and digital or analog docks. The dock state is\ntypically closely linked to the charging state, as many docks provide power to\ndocked devices.\n\nYour app affects the phone's update rate in the dock state. You can increase\nthe update frequency of a sports news app when it's in the desktop dock, or\ndisable your updates completely if the device is car docked. Conversely, you can\nmaximize your updates while car docked if your background service is updating\ntraffic conditions.\n\nThe dock state is also broadcast as a sticky\n[Intent](/reference/android/content/Intent),\nletting you query whether the device is docked and in which kind of dock if\nso.\n\nDetermine the current docking state\n-----------------------------------\n\nThe dock state details are included as an extra in a\n[sticky broadcast](/topic/security/risks/sticky-broadcast) of the\n[ACTION_DOCK_EVENT](/reference/android/content/Intent#ACTION_DOCK_EVENT)\naction. Because it's sticky, you can call\n[registerReceiver()](/reference/android/content/Context#registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)),\npassing in `null` as the broadcast receiver. The following snippet\nshows how to complete this process: \n\n### Kotlin\n\n```kotlin\nval dockStatus: Intent? = IntentFilter(Intent.ACTION_DOCK_EVENT).let { ifilter -\u003e\n context.registerReceiver(null, ifilter)\n}\n```\n\n### Java\n\n```java\nIntentFilter ifilter = new IntentFilter(Intent.ACTION_DOCK_EVENT);\nIntent dockStatus = context.registerReceiver(null, ifilter);\n```\n\nYou can extract the current docking status from the\n`EXTRA_DOCK_STATE` extra:\n\n\n### Kotlin\n\n```kotlin\nval dockState: Int = dockStatus?.getIntExtra(EXTRA_DOCK_STATE, -1) ?: -1\nval isDocked: Boolean = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED\n```\n\n### Java\n\n```java\nint dockState -1;\nif (dockStatus != null) {\n dockState = dockStatus.getIntExtra(EXTRA_DOCK_STATE, -1);\n}\nboolean isDocked = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;\n```\n\nDetermine the current dock type\n-------------------------------\n\nIf a device is docked, it can be docked in any of the following four\ndifferent dock types:\n\n- Car\n- Desk\n- Low-End (Analog) desk\n- High-End (Digital) desk\n\nThe latter two options are only introduced in Android API level 11, so it's\ngood practice to check for all three desk types when you are only interested in\nthe type of dock rather than it being digital or analog specifically: \n\n### Kotlin\n\n```kotlin\nval isCar: Boolean = dockState == EXTRA_DOCK_STATE_CAR\nval isDesk: Boolean = dockState == EXTRA_DOCK_STATE_DESK\n || dockState == EXTRA_DOCK_STATE_LE_DESK\n || dockState == EXTRA_DOCK_STATE_HE_DESK\n```\n\n### Java\n\n```java\nboolean isCar = dockState == EXTRA_DOCK_STATE_CAR;\nboolean isDesk = dockState == EXTRA_DOCK_STATE_DESK ||\n dockState == EXTRA_DOCK_STATE_LE_DESK ||\n dockState == EXTRA_DOCK_STATE_HE_DESK;\n```"]]