在asynctask运行时使用UI线程

时间:2016-03-02 14:19:37

标签: android android-asynctask

我正在更新我的UIpublishProgress()更明确,我正在使用从云中获取的歌曲更新我的RecyclerView,而我的列表已更新我想玩一两首歌,但我的广播不会发送到service,直到AsyncTask未完成,但highlight中的某些功能RecyclerView以及更新用户界面的功能。< / p>

broadcast正在运行时,有没有办法发送async task

2 个答案:

答案 0 :(得分:0)

听起来你想播放从云端返回的歌曲,也许是第一个从云中检索出来的歌曲。尝试使用处理程序将要播放的歌曲发送回UI线程。

将这样的代码添加到您的活动中:

Handler handler = new Handler() {
      @Override
      public void handleMessage(Message msg) {
          // Play song based on message
         }
     };

然后在你的publishProgress方法中发送一条消息,如下所示:

Message msg = handler.obtainMessage();
Bundle b = new Bundle();
b.putString("message", "value you need");
msg(b);    
sendMessage(msg)

答案 1 :(得分:0)

我通过强制asynctask与方法executeOnExecutor()

并行运行解决了我的问题