نمایش نتایج جستجوی قابل مرور
با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
همه برنامه ها باید از جستجوی صوتی پشتیبانی کنند. این صفحه نحوه بهبود بیشتر تجربه جستجو را با پشتیبانی از شروع جستجوهای بدون صدا و با نمایش لیستی از نتایج جستجو توضیح می دهد تا کاربران بتوانند نتیجه دیگری را انتخاب کنند. برای مثال، اگر نتیجه پیشنهادی مرتبط ترین نباشد.
برنامه رسانه شما میتواند نتایج جستجوی متنی را در Android Auto و Android Automotive OS (AAOS) ارائه دهد. این نتایج زمانی ظاهر می شوند که کاربر یک پرس و جوی جستجو را آغاز کند یا نتایج آخرین جستجو را مشاهده کند.
برای فعال کردن و ارائه این نتایج جستجو:
پشتیبانی جستجو را در روش onGetRoot
سرویس خود اعلام کنید.
برای مدیریت عبارات جستجوی کاربر، روش onSearch
را در سرویس مرورگر رسانه خود لغو کنید.
برای بهبود قابلیت مرور، نتایج جستجو را با استفاده از موارد عنوان سازماندهی کنید.
برنامه شما می تواند نتایج جستجوی متنی را ارائه دهد که هنگام شروع یک عبارت جستجو ظاهر می شوند. Android Auto و AAOS این نتایج را از طریق رابطهای جستجو یا از طریق هزینههایی که بر روی عبارتهای قبلی انجام شده است نشان میدهند. برای کسب اطلاعات بیشتر، به عملکردهای صوتی پشتیبانی مراجعه کنید.

شکل 1. نمای پخش با گزینه نتایج جستجو برای مشاهده موارد رسانه ای مربوط به جستجوی صوتی کاربر.
برای نشان دادن اینکه برنامه شما از نمایش نتایج جستجو پشتیبانی میکند، کلید ثابت BROWSER_SERVICE_EXTRAS_KEY_SEARCH_SUPPORTED
را در بسته اضافی که با روش onGetRoot
سرویس شما برگردانده شده است، قرار دهید، با نگاشت به Boolean true
.
کاتلین
import androidx.media.utils.MediaConstants
@Nullable
override fun onGetRoot(
@NonNull clientPackageName: String,
clientUid: Int,
@Nullable rootHints: Bundle
): BrowserRoot {
val extras = Bundle()
extras.putBoolean(
MediaConstants.BROWSER_SERVICE_EXTRAS_KEY_SEARCH_SUPPORTED, true)
return BrowserRoot(ROOT_ID, extras)
}
جاوا
import androidx.media.utils.MediaConstants;
@Nullable
@Override
public BrowserRoot onGetRoot(
@NonNull String clientPackageName,
int clientUid,
@Nullable Bundle rootHints) {
Bundle extras = new Bundle();
extras.putBoolean(
MediaConstants.BROWSER_SERVICE_EXTRAS_KEY_SEARCH_SUPPORTED, true);
return new BrowserRoot(ROOT_ID, extras);
}
برای ارائه نتایج جستجو، روش onSearch
را در سرویس مرورگر رسانه خود لغو کنید. Android Auto و AAOS زمانی که کاربر یک واسط عبارت جستجو یا استطاعت نتایج جستجو را فراخوانی میکند، عبارات جستجوی کاربر را به این روش ارسال میکنند.
برای مرور بیشتر نتایج جستجو، میتوانید از موارد عنوان استفاده کنید. به عنوان مثال، اگر برنامه شما موسیقی پخش می کند، می توانید نتایج جستجو را بر اساس آلبوم، هنرمند و آهنگ سازماندهی کنید. این قطعه کد پیاده سازی روش onSearch
را نشان می دهد:
کاتلین
fun onSearch(query: String, extras: Bundle) {
// Detach from results to unblock the caller (if a search is expensive).
result.detach()
object:AsyncTask() {
internal var searchResponse:ArrayList
internal var succeeded = false
protected fun doInBackground(vararg params:Void):Void {
searchResponse = ArrayList()
if (doSearch(query, extras, searchResponse))
{
succeeded = true
}
return null
}
protected fun onPostExecute(param:Void) {
if (succeeded)
{
// Sending an empty List informs the caller that there were no results.
result.sendResult(searchResponse)
}
else
{
// This invokes onError() on the search callback.
result.sendResult(null)
}
return null
}
}.execute()
}
// Populates resultsToFill with search results. Returns true on success or false on error.
private fun doSearch(
query: String,
extras: Bundle,
resultsToFill: ArrayList
): Boolean {
// Implement this method.
}
جاوا
@Override
public void onSearch(final String query, final Bundle extras,
Result<List<MediaItem>> result) {
// Detach from results to unblock the caller (if a search is expensive).
result.detach();
new AsyncTask<Void, Void, Void>() {
List>MediaItem> searchResponse;
boolean succeeded = false;
@Override
protected Void doInBackground(Void... params) {
searchResponse = new ArrayList<MediaItem>();
if (doSearch(query, extras, searchResponse)) {
succeeded = true;
}
return null;
}
@Override
protected void onPostExecute(Void param) {
if (succeeded) {
// Sending an empty List informs the caller that there were no results.
result.sendResult(searchResponse);
} else {
// This invokes onError() on the search callback.
result.sendResult(null);
}
}
}.execute()
}
/** Populates resultsToFill with search results. Returns true on success or false on error. */
private boolean doSearch(String query, Bundle extras, ArrayList<MediaItem> resultsToFill) {
// Implement this method.
}
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-08-12 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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"]],["تاریخ آخرین بهروزرسانی 2025-08-12 بهوقت ساعت هماهنگ جهانی."],[],[],null,["# Display browsable search results\n\nAll apps must support voice searches. This page describes how to further enhance\nthe search experience by supporting the initiation of searches without voice and\nby showing a list of search results so that users can pick another result. For\nexample, if the suggested result isn't the most relevant.\n\nYour media app can provide contextual search results in Android Auto and Android\nAutomotive OS (AAOS). These results appear when a user initiates a search query\nor views the results of the most recent search.\n\nTo enable and provide these search results:\n\n- Declare search support in your service's `onGetRoot` method.\n\n- Override the `onSearch` method in your media browser service to handle user\n search terms.\n\n- Organize Search results using title items for improved browsability.\n\nYour app can provide contextual search results that appear when a search query\nis started. Android Auto and AAOS show these results through search query\ninterfaces or through affordances that pivot on queries made earlier. To learn\nmore, see the [Support voice actions](/training/cars/media/voice-actions#support_voice).\n\n**Figure 1.** Playback view with a **Search results** option to view media items\nrelated to the user's voice search.\n\nTo indicate that your app supports the display of search results, include the\nconstant key [`BROWSER_SERVICE_EXTRAS_KEY_SEARCH_SUPPORTED`](/reference/androidx/media/utils/MediaConstants#BROWSER_SERVICE_EXTRAS_KEY_SEARCH_SUPPORTED()) in the extras\nbundle returned by your service's [`onGetRoot`](/reference/androidx/media/MediaBrowserServiceCompat#onGetRoot(java.lang.String,%20int,%20android.os.Bundle)) method, mapping to the\nBoolean `true`. \n\n### Kotlin\n\n import androidx.media.utils.MediaConstants\n\n @Nullable\n override fun onGetRoot(\n @NonNull clientPackageName: String,\n clientUid: Int,\n @Nullable rootHints: Bundle\n ): BrowserRoot {\n val extras = Bundle()\n extras.putBoolean(\n MediaConstants.BROWSER_SERVICE_EXTRAS_KEY_SEARCH_SUPPORTED, true)\n return BrowserRoot(ROOT_ID, extras)\n }\n\n### Java\n\n import androidx.media.utils.MediaConstants;\n\n @Nullable\n @Override\n public BrowserRoot onGetRoot(\n @NonNull String clientPackageName,\n int clientUid,\n @Nullable Bundle rootHints) {\n Bundle extras = new Bundle();\n extras.putBoolean(\n MediaConstants.BROWSER_SERVICE_EXTRAS_KEY_SEARCH_SUPPORTED, true);\n return new BrowserRoot(ROOT_ID, extras);\n }\n\nTo provide search results, override the [`onSearch`](/reference/androidx/media/MediaBrowserServiceCompat#onSearch(java.lang.String,android.os.Bundle,androidx.media.MediaBrowserServiceCompat.Result%3Cjava.util.List%3Candroid.support.v4.media.MediaBrowserCompat.MediaItem%3E%3E)) method in your media\nbrowser service. Android Auto and AAOS forward the user's search terms to this\nmethod when a user invokes a search query interface or **Search results**\naffordance.\n\nTo make search results more browsable, you can use [title items](/training/cars/media/create-media-browser/content-styles#group-items). For\nexample, if your app plays music, you can organize search results by album,\nartist, and song. This code snippet shows an implementation of the `onSearch`\nmethod: \n\n### Kotlin\n\n fun onSearch(query: String, extras: Bundle) {\n // Detach from results to unblock the caller (if a search is expensive).\n result.detach()\n object:AsyncTask() {\n internal var searchResponse:ArrayList\n internal var succeeded = false\n protected fun doInBackground(vararg params:Void):Void {\n searchResponse = ArrayList()\n if (doSearch(query, extras, searchResponse))\n {\n succeeded = true\n }\n return null\n }\n protected fun onPostExecute(param:Void) {\n if (succeeded)\n {\n // Sending an empty List informs the caller that there were no results.\n result.sendResult(searchResponse)\n }\n else\n {\n // This invokes onError() on the search callback.\n result.sendResult(null)\n }\n return null\n }\n }.execute()\n }\n // Populates resultsToFill with search results. Returns true on success or false on error.\n private fun doSearch(\n query: String,\n extras: Bundle,\n resultsToFill: ArrayList\n ): Boolean {\n // Implement this method.\n }\n\n### Java\n\n @Override\n public void onSearch(final String query, final Bundle extras,\n Result<List<MediaItem\u003e\u003e result) {\n\n // Detach from results to unblock the caller (if a search is expensive).\n result.detach();\n\n new AsyncTask\u003cVoid, Void, Void\u003e() {\n List\u003eMediaItem\u003e searchResponse;\n boolean succeeded = false;\n @Override\n protected Void doInBackground(Void... params) {\n searchResponse = new ArrayList<MediaItem\u003e();\n if (doSearch(query, extras, searchResponse)) {\n succeeded = true;\n }\n return null;\n }\n\n @Override\n protected void onPostExecute(Void param) {\n if (succeeded) {\n // Sending an empty List informs the caller that there were no results.\n result.sendResult(searchResponse);\n } else {\n // This invokes onError() on the search callback.\n result.sendResult(null);\n }\n }\n }.execute()\n }\n\n /** Populates resultsToFill with search results. Returns true on success or false on error. */\n private boolean doSearch(String query, Bundle extras, ArrayList<MediaItem\u003e resultsToFill) {\n // Implement this method.\n }"]]