I need when my app completely close (clear from backgrond) post my request to server and when got response service stop from working , this is my Service class :
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();
}
});
}
definition in manifest :
<service android:name="com.iranMobleh.OnClearFromRecentService" android:stopWithTask="false" />
and start service from MainActivity like below:
startService(new Intent(this, OnClearFromRecentService.class));
seem this code true but problem is when app closed it can't wait for retrofit response