如何将值传递给AsyncTask?

时间:2016-11-18 06:46:33

标签: android android-asynctask iptv

以下代码中的正确方法是什么(对我来说有点复杂的结构)从方法gotUrl()获取url到AsyncTask的doInBackground()方法,以便在onPostExecute()之后使用它doInBackground()方法是否已完成其任务?

public class PlayerActivity extends CustomActivity implements 
                                              ProblemListener{

     public class PlayChannel extends 
                      AsyncTask<CustomChangeChannel, String, String> {

          @Override
          protected String doInBackground(CustomChangeChannel... params) {
                 initOctoshapeSystem();

          return url;
          } 

          @Override
          protected void onPostExecute(String url){ 

          }

     }


     public void initOctoshapeSystem() {
           os = OctoStatic.create(this, this, null);

           os.setOctoshapeSystemListener(new OctoshapeSystemListener() {

                @Override
                public void onConnect() {
                     mStreamPlayer = setupStream(OCTOLINK);
                     mStreamPlayer.requestPlay();
                }
          });
     }



     public StreamPlayer setupStream(final String stream) {
        StreamPlayer sp = os.createStreamPlayer(stream);
        sp.setStatusChangedListener(new StatusChangedListener() {
            @Override
            public void statusChanged(byte oldStatus, 
                                      final byte newStatus) {
                runOnUiThread(new Runnable() {
                   @Override
                   public void run() {
                    //todo
                   }
                });
             }
        });

        sp.setListener(new StreamPlayerListener() {

            @Override
            public void gotUrl(String url) {
                //String to be passed
            }
        });

     return sp;
     }
}

2 个答案:

答案 0 :(得分:2)

AsyncTask<Param1, Param2, Param3>

Param 1是您传递到doInBackground方法的参数。

Param 2是您在AsyncTask工作时想要获得的。

Param 3就是你想得到的结果。

您可以将它们全部声明为Void。

AsyncTask<Void, Void, Void>

在您的情况下,您希望将String URL传递给doInBackground,因此:

AsyncTask<String, Void, Void>

调用执行时传递URL字符串。

mAsyncTask.execute("your url");

然后在doInBackground中获取它:

protected Void doInBackground(String... params) {
    String yourURL = params[0];
    return null;
}

答案 1 :(得分:0)

更改此

 public class PlayChannel extends 
                  AsyncTask<CustomChangeChannel, String, String>

到这个

 public class PlayChannel extends 
                  AsyncTask<String, String, String>

然后使用

PlayChannel channel  = new PlayChannel(url);