2017-05-19 34 views
2

我有一種方法在活動中執行一些操作,而非活動類。我想知道何時完成非活動類網絡操作,以便我可以再次調用該方法。但是當我打電話給非活動類的時候,方法就結束了。這裏是我的代碼,..如何控制活動方法流程,直到非活動類執行網絡操作

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"); 
} 

非活性類

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); 


    } 
    }; 

    } 


    } 

回答

1

你需要實現一個接口來獲取回調

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 
     } 
    }); 
} 
0

你可以使用LocalBroacastMan幾歲發送和你的活動註冊應用

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); 

      } 
     }; 
    } 
} 

內和接收信息,並確定接收器爲你的本地廣播

@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    
    } 
    } 
};