如何控制 Activity 方法流程直到非 activity class 执行网络操作

how to control the Activity method flow till non activity class perform network operation

我在 activity 中有一种方法,其中一些操作是从非 activity class 执行的。我想知道非 activity class 网络操作何时完成,以便我可以再次调用该方法。但是当我调用非 activity class 时,方法会转到最后。

这是我的代码

public void somemethod(){
            .
            .
            .
            .               
if (condition) {

    new RegenerateToken().generate(DriverActivity.this); // calling non activity class which perform some network operation

    //here i want to know that non activity class has performed the network operation so i can call this method again

    }       
            .
            .
            .
log.d("method","ending");
}

非activityclass

public class RegenerateToken {
    public void generate(Context context) {
    
    ExecuteServerReq executeServerReq = new ExecuteServerReq(context, client, Utilz.URL + "/authenticate", params, true, true);
    executeServerReq.execute();
    executeServerReq.getResponse = new ExecuteServerReq.GetResponse() {
    
    @Override
    public void onResponse(String objects) {
      Utilz.printLog("RegenerateTokenresponse", objects);
    }
    };
    }
}

您需要实现一个接口来获取回调

public class RegenerateToken {

public interface Callback {
    public void onResponce(String data);
}

public void generate(Context context,final Callback callBack) {
    ExecuteServerReq executeServerReq = new ExecuteServerReq(context, client, Utilz.URL + "/authenticate", params, true, true);
    executeServerReq.execute();
    executeServerReq.getResponse = new ExecuteServerReq.GetResponse() {

    @Override
    public void onResponse(String objects) {
        Utilz.printLog("RegenerateTokenresponse", objects);
        callBack.onResponce(objects);
    }};
}
}

回调实现

if(condition) {
    new RegenerateToken().generate(DriverActivity.this, new Callback() {  
        @Override
        public void onResponse(String objects) { //your data do ur processing
        }
    });
}

您可以使用 LocalBroacastManager 在您的应用程序中发送和接收信息

public class RegenerateToken {

    public void generate(Context context) {
    //your code
        executeServerReq.getResponse = new ExecuteServerReq.GetResponse() {

            @Override
            public void onResponse(String objects) {
                Utilz.printLog("RegenerateTokenresponse", objects);
                Intent intent = new Intent("Respone");
                if(expected responce){    
                    intent.putExtra("result", "Successful");
                }
                else{
                    intent.putExtra("result", "UnSuccessful");     
                }
                LocalBroadcastManager.getInstance(this).sendBroadcast(iIntent);

            }
        };
    }
}

然后在您的 Activity 注册并为您的本地广播定义接收器

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    //Register the custom Broadcast "Response"

    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
    new IntentFilter("Response"));
}

// Our handler for received Intents. This will be called whenever an Intent
// with an action named "Response" is broadcasted.
private BroadcastReceiver myMessageReceiver = new BroadcastReceiver() {
@Override
    public void onReceive(Context context, Intent intent) {
    String response= intent.getStringExtra("result");
    if(response.equals("Successful")){
        //on successful execution
        // call your method again or do whatever you wish
    }else{
        //on otherwise            
    }
  }
};