2016-08-17 4 views
0

안녕하세요. 끔찍한 상황이 있습니다. 세션 내 위치 공유 로직을 만들고 있습니다. mysql 서버에서 세션을 유지하고 있습니다. 해당 활동에 안드로이드가 안타를 치면 그에 따라 사용자 정보가 삽입됩니다. 안드로이드가 그 활동을 떠날 때 세션이 반대편으로 버려지도록 그 칼럼을 삭제합니다. 하나의 이슈가 될 때까지는 모든 것이 좋습니다 .Android는 앱에서 콜백을 제공하지 않고, 앱을 끌고 간다는 앱이 완전하게 죽었습니다. 나는 원하는 활동에 도달하자마자 서비스와 시작 서비스를 사용하고 있습니다. 서비스에서 나는 앱이 recents에서 그것을 스 와이프하여 죽은 즉시 알려주는 onTaskRemoved()라는 간단한 것을 가지고 있습니다. 내가 원하는 열을 제거하기 위해 내 서버에 전화를 걸고 싶습니다. 전화가 연결되지 않고 응답을받지 못합니다. 그러나 onDe에서는 STROY()와 같은 expected.Actually 여기서 일하는 모든 코드Android onTaskRemoved() 웹 서비스 호출

@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(); 
     } 
    }); 
} 

i가 인쇄되는 경우에만 로그 추측을 통해 통화가 얻을 수 없을 것입니다 단서 무슨 일이 more..Anyone 아이디의 로그 및 아무것도입니다 어떻게 된거 야?

+0

안녕하세요.이 문제에 대한 해결책을 얻었습니까? 고맙습니다. – user1510006

+1

아직 :(coudl을 통해 얻지 못했습니다 –

+0

OK, 감사합니다 :) – user1510006

답변

3
/** 
* 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 호출을 수행 할 수 없다는 동일한 문제에 직면했습니다. 그럼에도 많은 연구를했지만 해결책이 없습니다. 그래서, 의도적으로 내부에 배치 된 일부 엑스트라로 웹 서비스를 다시 시작하는 아이디어를 마침내 얻었습니다. 이러한 방식으로 API 호출을 실행하기 위해 서비스가 다시 시작되면 구분할 수 있습니다.

+0

그것은 나를 도왔습니다. 하지만 의도가 null이 아니면 물건을 처리하는지 먼저 확인했습니다. 다음은 내 코드입니다. @Override public int onStartCommand (intent intent, int flags, int startId) { if (intent! = null) { 번들 번들 = intent.getExtras(); if (번들! = null) { 최종 문자열 로그 아웃 = bundle.getString ("로그 아웃"); if (logout! = null) { logoutAndStopServiceRightAway(); 반환 START_NOT_STICKY; } } } 반환 START_STICKY; } – user2304819