2014-04-09 2 views
0

저는 Android 애플리케이션에서 Observer/Observable 패턴을 구현했습니다.하지만 통지를하면 수신자는 전화를받지 못합니다. Observable pattern not android in notroid에서 통지하지 않습니다.

내 코드입니다 :

관찰자 서버가 꺼져 있기 때문에 호출이 실패 할 경우, 전화 휴식 서비스를위한 클래스가, 또는 다른 문제는, 내가 나머지 서버 IP를 변경하는 int 값을 증가 에서 관찰 가능한 및 관찰 가능한 내 업데이트 재정의 메서드를 호출하지만, 그것을 해달라고해야합니다

public class LoginApiCall extends ApiCallBase<Object> implements Observer { 

     private String team; 
     private String password; 
     private String deviceId; 

     private LoginIntents loginIntents; 

     public LoginApiCall(Context context, String team, String password, String deviceId, ApiResponseListener listener) { 
      super(context, listener); 
      this.team = team; 
      this.password = password; 
      this.deviceId = deviceId; 
      loginIntents = LoginIntents.getInstance(); 
      loginIntents.addObserver(this); 
     } 

     @Override 
     protected void doWork() { 

      LoginData login = new LoginData(team, password, deviceId); 

      Gson gson = new Gson(); 
      String value = gson.toJson(login); 

      try { 
       ByteArrayEntity entity = new ByteArrayEntity(value.getBytes("UTF-8")); 

       Api.post(context, "teamLogin", entity, new JsonHttpResponseHandler() { 
        @Override 
        public void onSuccess(JSONObject jsonObject) { 
         apiSuccess(jsonObject); 
        } 

        @Override 
        public void onFailure(Throwable throwable, JSONObject jsonObject) { 
         throwable.printStackTrace(); 
         apiError(new ApiException()); 
        } 

        @Override 
        public void onFailure(Throwable throwable, String content) { 
         if (throwable.getCause() instanceof ConnectTimeoutException) { 
          System.out.println("Connection timeout !"); 
         } 
         loginIntents.increaseIntents(); 
         throwable.printStackTrace(); 
         apiError(new ServerException()); 
        } 
       }); 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } 

     } 

     @Override 
     public void update(Observable observable, Object data) { 
      execute(); 
     } 
} 

를이는 의도가 0에서 1로 속성이 증가 할 때, 관찰자에게 통지하고 뭔가를해야한다고, 관찰 할 수 있습니다 :

public class LoginIntents extends Observable { 

    private static volatile LoginIntents instance = null; 
    private int intents = 0; 

    private LoginIntents() { 
    } 

    public static LoginIntents getInstance() { 
     if (instance == null) { 
      instance = new LoginIntents(); 
     } 
     return instance; 
    } 

    public int getIntents() { 
     return instance.intents; 
    } 

    public void increaseIntents() { 
     instance.intents++; 
     if (intents == 1) { 
      notifyObservers(); 
      setChanged(); 
     } 
    } 
} 

뭐가 잘못 되었나요? notifyObservers 메서드는 디버그 및 setChanged도 호출되지만 Observer의 업데이트 메서드에 입력하지 마십시오.

답변