多个OkHttp发布请求后的回调/事件

时间:2016-03-04 19:10:26

标签: java android android-service okhttp android-broadcastreceiver

我的应用程序有一个function ShellExecute(hwnd: HWND; lpOperation: string; lpFile: string; lpParameters: string; lpDirectory: string; nShowCmd: Integer): THandle; external 'ShellExecuteW@shell32.dll stdcall'; function InitializeSetup(): Boolean; var HasLog: Boolean; Params: string; I: Integer; S: string; RetVal: Integer; begin HasLog := False; Params := ''; for I := 1 to ParamCount do begin S := ParamStr(I); if CompareText(Copy(S, 1, 5), '/LOG=') = 0 then begin HasLog := True; break; end; { Do not pass our /SL5 switch } if CompareText(Copy(S, 1, 5), '/SL5=') = 0 then begin Params := Params + AddQuotes(S) + ' '; end; end; Result := True; if HasLog then begin Log('Log specified, continuing.'); end else begin { add selected language, so that user is not prompted again } Params := Params + ' /LANG=' + ActiveLanguage; { force logging } Params := Params + ' /LOG="' + ExpandConstant('{%TEMP}\ProductInstall.log') + '"'; Log(Format('Log file not specified, restarting setup with [%s]', [Params])); RetVal := ShellExecute(0, '', ExpandConstant('{srcexe}'), Params, '', SW_SHOW); Log(Format('Restarting setup returned [%d]', [RetVal])); if RetVal > 32 then begin Log('Restart with logging succeeded, aborting this instance'); Result := False; end else begin Log(Format('Restarting with logging failed [%s], keeping this instance', [ SysErrorMessage(RetVal)])); end; end; end; ,它是使用BroadCastReceiver以编程方式注册的。代码registerReceiver(receiver, filter)放在registerReceiver(receiver, filter)类中,因此它具有全局访问权限。Application启动BroadCastReceiver,向服务器发送多个OkHttp POST请求。当多个POST请求成功时,我想取消注册Service的实例。怎么办呢?

MyService.java:

BroadCastReceiver

MyReciever.java:

public class MyService extends Service {

  @Override
  public void onCreate() {
     for(int i=0;i<size;i++){
       /....Multiple POST Requests..../
     }

     Callback/Event(){ execute unregisterReceiver();}
  }

  @Override
  public void onDestroy() {

  }
}

1 个答案:

答案 0 :(得分:0)

好的,我只是使用一个计数器变量来解决它。

public class MyService extends Service {

 int counter;
 @Override
 public void onCreate() {
  counter = size ;
  for(int i=0;i<size;i++){
    /....Multiple POST Requests..../

    OnResponse: 
    counter-- 
    if(counter ==0){ 
      execute unregisterReceiver();
      }

   }

 }

 @Override
 public void onDestroy() {

   }
 }