1.在app的ServiceModule中加入要使用的service
@Singleton
@Provides
RtcService provideRtcService(Retrofit retrofit) {
return retrofit.create(RtcService.class);
}
2.在需要使用service的activity中加入
@Inject
RtcService rtcService;
protected void onCreate(Bundle savedInstanceState) {
AndroidInjection.inject(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rtc);
}
3.在app的uimodel中对要使用的acitivity加入
@ActivityScope
@ContributesAndroidInjector
abstract RTCActivity contributeRTCActivityInjector();
4.使用
public interface RtcService {
// 加入房间
@POST("api.php?m=message.joinRoom")
@FormUrlEncoded
LiveData<ApiResponse<BaseResponse<CallVo>>> joinRoom(@FieldMap HashMap<String, String> paramMap);
}
rtcService.joinRoom(params).observe((LifecycleOwner) RTCActivity.this, new Observer<ApiResponse<BaseResponse<CallVo>>>() {
@Override
public void onChanged(@Nullable ApiResponse<BaseResponse<CallVo>> baseResponseApiResponse) {
BaseResponse<CallVo> callVoBaseResponse = baseResponseApiResponse.body;
callVo = callVoBaseResponse.getRst();
}
});