Android onTaskRemoved() 调用网络服务

Android onTaskRemoved() call to webservice

很好 day.I 很糟糕 situation.I 我正在 session.I 中创建一个位置共享逻辑 在 mysql.When android 命中的服务器上保持该会话activity,我相应地插入用户 information.When android 离开 activity 当然,我删除了该列,因此会话被放弃,因为相反 side.All 很好,直到一个 issue.Android 不提供从最近刷过的应用程序的回调,这意味着应用程序被杀死 completelly.I 找到了解决方法 there.I 我正在使用服务并在达到我想要的状态后立即启动服务 activity.In 服务我有一个叫做 onTaskRemoved() 的简单的东西,它会在应用程序被从 recents.All 滑动到我想调用我的服务器以删除 column.The 呼叫不仅会通过,我永远不会在那里收到任何响应,但在 onDestroy() 中,一切都像 expected.Actually 一样工作,这里是代码

 @Override
public void onTaskRemoved(Intent rootIntent) {
    destroySession();
    super.onTaskRemoved(rootIntent);
}

private void destroySession() {
    Log.d("Fsafasfsafasfas", "destroySession: " + opponentId + " my user id" + sharedHelper.getUserId());
    Call<ResponseBody> locationCall = Retrofit.getInstance().getInkService().requestFriendLocation(sharedHelper.getUserId(), opponentId, "", "", Constants.LOCATION_REQUEST_TYPE_DELETE);
    locationCall.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response == null) {
                destroySession();
                return;
            }
            if (response.body() == null) {
                destroySession();
                return;
            }
            try {
                String responseBody = response.body().string();
                Log.d("Fsafasfsafasfas", "onResponse: " + responseBody);
                JSONObject jsonObject = new JSONObject(responseBody);
                boolean success = jsonObject.optBoolean("success");
                if (success) {
                    stopSelf();
                } else {
                    destroySession();
                }
            } catch (IOException e) {
                stopSelf();
                e.printStackTrace();
            } catch (JSONException e) {
                stopSelf();
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            destroySession();
        }
    });
}

我猜这个电话永远打不通,因为唯一打印的日志是 id 的日志,仅此而已。有人知道发生了什么事吗?我该如何处理这种情况?

/**
 * Created by Parag on 01/05/2017.
 */

public class AppService extends android.app.Service {
    public static final String TAG=AppService.class.getName();
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Bundle bundle=intent.getExtras();
        if(bundle!=null){
            final String logout=bundle.getString("logout");
            final String driverLoginId=bundle.getString("driverLoginId");
            if(logout!=null&&driverLoginId!=null){
                Toast.makeText(this, "logging out", Toast.LENGTH_SHORT).show();
                Log.e(TAG,"Logging out");
                Log.e(TAG,"Inside driverLogout "+driverLoginId);
                Call<LogoutResponse> call = RestHandler.getApiService().driverLogout(driverLoginId);
                call.enqueue(new Callback<LogoutResponse>() {
                    @Override
                    public void onResponse(Call<LogoutResponse> call, Response<LogoutResponse> response) {
                        //close the service on receiving response from API
                        Log.e("Response : ",response.body().getStatus()+"");
                        AppService.this.stopSelf();
                    }
                    @Override
                    public void onFailure(Call<LogoutResponse> call, Throwable t) {
                        //close the service on receiving response from API
                        AppService.this.stopSelf();
                    }
                });

            }else{
                //Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
                Log.e(TAG,"DriverLoginId : "+driverLoginId);
                Log.e(TAG,"Logout : "+logout);
            }
        }else{
            //Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
            Log.e(TAG,"Service Start");
        }
        return super.onStartCommand(intent,flags,startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {

        Log.e(TAG,"Service Stop");
        UserLocalStore userLocalStore=new UserLocalStore(AppService.this);
        Log.e("USER DATA :",userLocalStore.fetchUserData().toString());
        Intent restartServiceTask = new Intent(getApplicationContext(),this.getClass());
        restartServiceTask.setPackage(getPackageName());
        restartServiceTask.putExtra("logout","true");
        restartServiceTask.putExtra("driverLoginId",userLocalStore.fetchUserData().getUserId());
        PendingIntent restartPendingIntent =PendingIntent.getService(getApplicationContext(), 1,restartServiceTask, PendingIntent.FLAG_ONE_SHOT);
        AlarmManager myAlarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        myAlarmService.set(
                AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + 1000,
                restartPendingIntent);
        super.onTaskRemoved(rootIntent);
    }


}

我也面临同样的问题,即无法从 onTaskRemoved 方法进行任何 API 调用。 所以,我也研究了很多,但没有找到解决办法。所以,我终于想到了重新启动 Web 服务,并在意图中放置一些额外的东西。通过这种方式,您可以区分服务何时重新启动以执行您的 API 调用。

我通过取消线程策略的严格模式解决了这个问题。所以http请求可以从主线程完成。

 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
 StrictMode.setThreadPolicy(policy);

在正常情况下不推荐这样做,但是当应用程序崩溃或关闭时,我想发送一个 http 请求是可以的。