【发布时间】:2018-08-13 04:17:07
【问题描述】:
当我的应用程序完全关闭时(从背景中清除)我需要将我的请求发布到服务器并且当响应服务停止工作时,这是我的服务类:
public class OnClearFromRecentService extends Service {
private SharedPreferences prefs;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ClearFromRecentService", "Service Started");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("ClearFromRecentService", "Service Destroyed");
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("ClearFromRecentService", "END");
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(App.baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
RequestInterface requestInterface = retrofit.create(RequestInterface.class);
String authorization = prefs.getString("token_type","")+" "+prefs.getString("access_token","");
Call<JsonArray> response = requestInterface.AppOff(ChatList.offline_user,authorization);
response.enqueue(new Callback<JsonArray>() {
@Override
public void onResponse(Call<JsonArray> call, retrofit2.Response<JsonArray> response) {
String asd = "asd";
stopSelf();
}
@Override
public void onFailure(Call<JsonArray> call, Throwable t) {
Log.d(t.getLocalizedMessage(),"failed");
stopSelf();
}
});
}
清单中的定义:
<service android:name="com.iranMobleh.OnClearFromRecentService" android:stopWithTask="false" />
并从 MainActivity 启动服务,如下所示:
startService(new Intent(this, OnClearFromRecentService.class));
似乎这段代码是真的,但问题是当应用关闭时它不能等待改造响应
【问题讨论】: