Android onTaskRemoved()对webservice的调用

Android onTaskRemoved()对webservice的调用,android,service,kill-process,Android,Service,Kill Process,你好。我遇到了可怕的情况。我正在会话中创建位置共享逻辑。我将会话保存在mysql上的服务器上。当android点击该活动时,我会相应地插入用户信息。当android退出活动时,我当然会删除该列,这样会话就被对方放弃了。在出现一个问题之前,一切都很好。android不会为最近刷过的应用程序提供回调,meanging应用程序被完全删除。我在附近找到了一个工作。我正在使用一个服务,并在它达到我想要的活动时启动它。在服务中,我有一个简单的东西叫做onTaskRemoved()当应用程序从最近的位置被刷杀

你好。我遇到了可怕的情况。我正在会话中创建位置共享逻辑。我将会话保存在mysql上的服务器上。当android点击该活动时,我会相应地插入用户信息。当android退出活动时,我当然会删除该列,这样会话就被对方放弃了。在出现一个问题之前,一切都很好。android不会为最近刷过的应用程序提供回调,meanging应用程序被完全删除。我在附近找到了一个工作。我正在使用一个服务,并在它达到我想要的活动时启动它。在服务中,我有一个简单的东西叫做onTaskRemoved()当应用程序从最近的位置被刷杀时,它会立即通知我。在我想调用我的服务器以删除列之前,一切都很好。调用不仅会通过,我永远不会在那里收到任何响应,而是在onDestroy()中,一切都按预期工作。实际上,下面是代码

 @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();
        }
    });
}
@覆盖
公共void onTaskRemoved(Intent rootIntent){
销毁会话();
super.onTaskRemoved(rootIntent);
}
专用会话(){
Log.d(“Fsafasfsafasfas”,“destroySession:+opponentId+“我的用户id”+sharedHelper.getUserId());
Call locationCall=Reformation.getInstance().getInkService().requestFriendLocation(sharedHelper.getUserId(),opponentId,“,”,Constants.LOCATION\请求类型\删除);
locationCall.enqueue(新的回调(){
@凌驾
公共void onResponse(调用、响应){
如果(响应==null){
销毁会话();
返回;
}
if(response.body()==null){
销毁会话();
返回;
}
试一试{
String responseBody=response.body().String();
Log.d(“Fsafasfsafasfas”,“onResponse:+responseBody”);
JSONObject JSONObject=新的JSONObject(ResponseBy);
布尔成功=jsonObject.optBoolean(“成功”);
如果(成功){
stopSelf();
}否则{
销毁会话();
}
}捕获(IOE异常){
stopSelf();
e、 printStackTrace();
}捕获(JSONException e){
stopSelf();
e、 printStackTrace();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
销毁会话();
}
});
}
我想电话永远也打不通,因为唯一打印的日志是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);
    }


}
*由Parag于2017年5月1日创建。 */ 公共类AppService扩展了android.app.Service{ 公共静态最终字符串标记=AppService.class.getName(); @凌驾 公共int onStartCommand(Intent Intent、int标志、int startId){ Bundle=intent.getExtras(); if(bundle!=null){ 最终字符串注销=bundle.getString(“注销”); final String driverLoginId=bundle.getString(“driverLoginId”); if(注销!=null&&driverLoginId!=null){ Toast.makeText(这是“注销”,Toast.LENGTH_SHORT).show(); Log.e(标签“注销”); Log.e(标签,“内部驱动器登录”+driverLoginId); Call Call=RestHandler.getApiService().driverLogout(driverLoginId); call.enqueue(新回调(){ @凌驾 公共void onResponse(调用、响应){ //从API接收响应时关闭服务 Log.e(“Response:,Response.body().getStatus()+”); AppService.this.stopSelf(); } @凌驾 失败时公共无效(调用调用,可丢弃的t){ //从API接收响应时关闭服务 AppService.this.stopSelf(); } }); }否则{ //Toast.makeText(这是“服务启动”,Toast.LENGTH_SHORT).show(); Log.e(标签“DriverLoginId:+DriverLoginId”); Log.e(标签“注销:”+注销); } }否则{ //Toast.makeText(这是“服务启动”,Toast.LENGTH_SHORT).show(); Log.e(标签“服务启动”); } 返回super.onStartCommand(intent、flags、startId); } @可空 @凌驾 公共IBinder onBind(意向){ 返回null; } @凌驾 公共void onTaskRemoved(Intent rootIntent){ 日志e(标签“服务站”); UserLocalStore UserLocalStore=新的UserLocalStore(AppService.this); Log.e(“用户数据:”,userLocalStore.fetchUserData().toString()); Intent restartServiceTask=newintent(getApplicationContext(),this.getClass()); setPackage(getPackageName()); restartServiceTask.putExtra(“注销”、“真”); restartServiceTask.putExtra(“driverLoginId”,userLocalStore.fetchUserData().getUserId()); PendingEvent RestartPendingEvent=PendingEvent.getService(getApplicationContext(),1,restartServiceTask,PendingEvent.FLAG_ONE_SHOT); AlarmManager myAlarmService=(AlarmManager)getApplicationContext().getSystemService(Context.ALARM\u服务); myAlarmService.set( AlarmManager.u实时, SystemClock.elapsedRealtime()+1000, 餐厅(帐篷); super.onTaskRemoved(rootIntent); } }
我还面临着同样的问题,无法从onTaskRemoved方法进行任何API调用。 所以,我也做了很多研究,但没有找到解决办法。因此,我终于有了重新启动web服务的想法,在intent中添加了一些额外的内容。
 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
 StrictMode.setThreadPolicy(policy);