【问题标题】:Pass different type as activity将不同类型作为活动传递
【发布时间】:2014-01-30 22:38:39
【问题描述】:

我有几个活动需要执行 HTTP 请求(发送 JSON 请求并返回另一个 JSON 对象)。

我的想法是为所有这些请求共享一个 AsyncTask。我将 Activity 作为参数传递,以便在请求执行完成后调用方法。

我想再向我的 AsyncTask 传递一个参数,该参数将是我的 Activity 的类(MainActivity.class、SecondActivity.class),然后使用该信息将 Activity 转换为正确的类型,然后再调用方法(所有活动的方法名称都相同)。

我也可以使用我的回调方法创建一个接口,但我不确定这是否也行不通。

这行得通还是我的方法在这里错了?

感谢您的反馈。

我的代码:

公共类 HTTPReq 扩展 AsyncTask {

private MainActivity callerActivity;

@Override
protected String doInBackground(Object... params) {
    String data = (String) params[0];
    String cookie = (String) params[1];
    callerActivity = (MainActivity) params[2];

...
}

@Override
protected void onPostExecute(String result) {
    callerActivity.ProcessHTTPReqAnswer(result);
    super.onPostExecute(result);
}

}

【问题讨论】:

    标签: android android-activity android-asynctask


    【解决方案1】:

    Aswins 的回答并不糟糕,但仍然不是最有效的方法。

    声明一个具有回调方法的接口。将该接口的一个实例传递给您的 asynctask,然后根据我下面的示例让异步任务调用它(如果它存在)

    界面:

      public interface IMyCallbackInterface {
         void onCallback(String result);
      }
    

    异步任务:

      public MyAsyncTask extends AsyncTask<..., String> {
    
          private IMyCallbackInterface mCallback;
    
    
          public MyAsyncTask(..., IMyCallbackInterface callback) {
               mCallback = callback;
          }
    
          protected String doInBackground(Object... params) {
             ....
          } 
    
          protected void onPostExecute(String result) {
             super.onPostExecute(result);
             if (mCallback != null) {
                  mCallback.onCallback(result);
             }
          }
    

    活动:

         public MyActivity extends Activity {
    
              private void someMethod(){
                   new MyAsyncTask(..., new IMyCallbackInterface() {
    
                         public void onCallback(String result) {
                               //TODO use the result to do whatever i need
                               //I have access to my aactivity methods and member variables here
                         }
                   }.execute();
              }
          }
    

    【讨论】:

      【解决方案2】:

      这样做是错误的。您应该使用广播接收器。完成 AsyncTask 后,将结果作为广播发送出去。每个活动都会听取他们感兴趣的结果。这样就没有人需要保留对危险活动的引用。

      这是一个例子。

      IntentFilter filter = new IntentFilter();
      filter.addAction("result1");
      LocalBroadcastManager.getInstance(getSupportActivity()).registerReceiver(new CustomBroadcastReceiver(), filter);
      

      在 AsyncTask 中,这样做

      @Override
      protected void onPostExecute(String result) {
          Intent intent = new Intent("result1").putExtra(
                      "data", result);
          LocalBroadcastManager.getInstance(getSupportActivity()).sendBroadcast(intent);
          super.onPostExecute(result);
      }
      

      回到活动中,这样做

      private class CustomBroadcastReceiver extends BroadcastReceiver {
      
          @Override
          public void onReceive(Context context, Intent intent) {
              Bundle bundle = intent.getExtras();
              if ("result1".equalsIgnoreCase(intent.getAction())) {
                  String result = bundle.getString("data");
                  // Process the result here.
              } 
          }
      }
      

      【讨论】:

      • 我的 Asynctask 是在一个单独的类中定义的,这样我就可以从不同的活动中调用它。我不能使用“getSupportActivity()”来获取主要活动,我需要将一个“上下文”对象传递给 Asyntask,然后它就可以正常工作了。
      猜你喜欢
      • 1970-01-01
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 2022-07-24
      • 1970-01-01
      相关资源
      最近更新 更多