简体   繁体   English

自动:在进行后台呼叫时显示进度

[英]Auto: Showing the progress while doing a background call

We have an audio app and we want to add Android Auto capability to it. 我们有一个音频应用程序,我们想要添加Android Auto功能。 The app basically has a list of radio streams and the user can select which one to play. 该应用程序基本上有一个无线电流列表,用户可以选择播放哪一个。

In order to obtain the streams, I need to call a web service and obtain the streams, while that is occurring I wish to show an indeterminate progress at least on the hamburger menu like Spotify does when it's loading content. 为了获得流,我需要调用一个Web服务并获取流,当发生这种情况时,我希望至少在汉堡菜单上显示不确定的进度,就像Spotify在加载内容时所做的那样。

At this moment if I have already downloaded the streams, on my MediaBrowserServiceCompat when Auto calls onLoadChildren my Result<List<MediaBrowserCompat.MediaItem>> result object is populated with the MediaItems, if I don't have them I just send an empty array of MediaItems and call the API, once it has finished I call notifyChildrenChanged on the Service and the list of streams appear, but until then I have a "No items" message. 此时,如果我已经下载了流,则在我的MediaBrowserServiceCompat自动调用onLoadChildren我的Result<List<MediaBrowserCompat.MediaItem>> result对象填充了MediaItems,如果我没有它们,我只发送一个空数组MediaItems并调用API,一旦完成,我在服务上调用notifyChildrenChanged并显示流列表,但在此之前我有一个“无项目”消息。

I could not find on Android Developer site a way to set the children menu to show a loading and I could not find a sample app that does that. 我在Android开发者网站上找不到设置子菜单以显示加载的方法,我找不到这样做的示例应用程序。 Does anyone know if there's way, maybe when onGetRoot is called to let Auto know it has to wait before calling onGetRoot and onLoadChildren ? 有谁知道,如果有办法,说不定什么时候onGetRoot被调用,让自动知道它调用之前等待onGetRootonLoadChildren I also tried calling the API then calling myself onGetRoot but it did not work. 我也尝试调用API然后调用自己onGetRoot但它不起作用。

Thanks in advance. 提前致谢。

OnGetRoot should return immediately, because it actually carries no content. OnGetRoot应立即返回,因为它实际上不包含任何内容。

onLoadChildren though is where the content is loaded from, and it supports async loading on any content level. onLoadChildren虽然是从中加载内容的地方,但它支持在任何内容级别上进行异步加载。 When your fetching the streams, don't send an empty array of MediaItems before you call the API. 在获取流时,在调用API之前不要发送一个空的MediaItem数组。 Instead, fetch the streams from within onLoadChildren(). 而是从onLoadChildren()中获取流。 Call results.detach(), fetch the streams in another thread, then call results.sendResult when the content is available. 调用results.detach(),在另一个线程中获取流,然后在内容可用时调用results.sendResult。 The media content browser will show a progress spinner while waiting for the async sendResult call 媒体内容浏览器将在等待异步sendResult调用时显示进度微调器

Something like this will do the trick. 像这样的东西可以解决问题。

@Override
public void onLoadChildren(@NonNull final String parentMediaId, @NonNull final Result<List<MediaItem>> result) {
    if (/* if music library is ready, return immediately */) {
        result.sendResult(getChildren(parentMediaId, getResources()));
    } else {
        // otherwise, return results when the streams are ready
        result.detach();
        someMusicProvider.retrieveMediaAsync(new MusicProvider.Callback() {
            @Override
            public void onStreamsReady(boolean success) {
                result.sendResult(...);
            }
        });
    }
}

Would the use of ProgressBar be your answer? ProgressBar的使用会成为您的答案吗? Simply add the view into any layout you require 只需将视图添加到您需要的任何布局中

  <ProgressBar
   id=“progressBar”
   width=“wrap_view”
   height=“wrap_view”
   android:indeterminate=“true”
   android:centreInParent=“true”
    visibility=“gone” />

So once you have that in your layout, add a ProgressBar object and assign it on oNCreateView() 所以,一旦你在布局中有了它,添加一个ProgressBar对象并在oNCreateView()上分配它

progressBar = findViewById(R.id.progressBar);

and simply as you begin the call request 并且只需在您开始呼叫请求时

   progressBar.setVisibility(VISIBLE);

And on the callback for that webservice (if you don't have one I recommend you create a quick “success/failure” callback) just call 在该webservice的回调上(如果你没有我建议你创建一个快速的“成功/失败”回调)只需要调用

     progressBar.setVisibility(GONE);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM