2014-07-17 2 views
0

두 환경 설정을 모니터링하는 부분에 OnSharedPreferenceChangeListener이 있습니다. 그 중 하나가 변경되면 AsyncTask가 호출됩니다. 세부 사항은 전부는 아니지만 AsyncTask가 수행하는 모든 작업은 XML 파일을 구문 분석하고 리턴 및 int를 수행하는 것입니다.OnSharedPreferenceChangeListener 등록/등록 해제

코드는 정상적으로 작동하지만 가능한 경우 수정하고 싶은 사소한 문제가 있음을 확인했습니다.

환경 설정에 들어가서 청취자와 "묶는"두 가지 중 하나를 변경하면 환경 설정 대화 상자에서 "확인"을 클릭하면 약간의 지연이 있습니다. 로그에서 실제로 조각으로 돌아 가기 전에 AsyncTask를 호출하고 있음을 추론 할 수 있습니다 (예 : 전체 조각 활동의 설정 조각에서 AsyncTask를 호출 중임).

나는 이것이 청취자와 관련이 있다고 가정하지만, 나는 확실히 모른다.

제안 사항? 그것은 리스너가 호출 될 때 나타납니다

:

SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() { 
     @Override 
     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 
      Log.d(LOGCAT_TAG, "(ON SHARED PREFERENCE CHANGE LISTENER) Setting changed: " + key); 

      if(key.equals(Reference.KEY_PREF_PLAYER_NAME) || key.equals(Reference.KEY_PREF_UNI)) { 

       //Load up the new prefs 
       String newPlayerName = sharedPreferences.getString(Reference.KEY_PREF_PLAYER_NAME, ""); 
       String newUni = sharedPreferences.getString(Reference.KEY_PREF_UNI, ""); 

       //Get the new iD 
       int tempInt = getUserIDFromAsyncTask(context, newPlayerName, newUni); 

       //If we still did not get an ID 
       if (tempInt == 0) { 
        Log.d(LOGCAT_TAG, "(ON SHARED PREFERENCE CHANGE LISTENER) A setting was changed. Attempted to get new ID to no avail."); 
        mTitleTextView.setText("Sorry. I couldn't find your new user ID."); 
       } 

       //We got a new ID 
       else { 
        Log.d(LOGCAT_TAG, "(ON SHARED PREFERENCE CHANGE LISTENER) New ID: " + tempInt); 
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); 
        SharedPreferences.Editor editor = sharedPreferences.edit(); 
        editor.putInt(Reference.KEY_PREF_USER_ID, tempInt); 
        editor.commit(); 
       } 
      } 
     } 
    }; 

    //Register the listener 
    //TODO: Change/Move this because it may become garbage collected 
    sharedPreferences.registerOnSharedPreferenceChangeListener(preferenceChangeListener); 

당신은이 로그 뷰에서 문제를 일으키는 코드임을 알 수 있습니다 : 아래

이 문제의 원인이되는 코드 3 번 코드의 라인을 따라, 한 번 난 단지 설정을 변경하더라도 (오픈 설정, 설정을 클릭, 새로운 텍스트로 입력 괜찮 클릭)

07-17 13:14:19.162 18232-18232/com.example.game D/HomeFragment﹕ (ON SHARED PREFERENCE CHANGE LISTENER) Setting changed: game_player_name 
07-17 13:14:20.063 18232-18241/com.example.game D/AbsListView﹕ [unregisterDoubleTapMotionListener] 
07-17 13:14:20.063 18232-18879/com.example.game D/dalvikvm﹕ GC_FOR_ALLOC freed 1735K, 28% free 19420K/26952K, paused 28ms, total 29ms 
07-17 13:14:20.063 18232-18241/com.example.game I/MotionRecognitionManager﹕ .unregisterListener :/listener count = 0->0, 
07-17 13:14:20.063 18232-18241/com.example.game D/AbsListView﹕ unregisterIRListener() is called 
07-17 13:14:20.133 18232-18879/com.example.game I/ParsePlayers﹕ Found matching ID @ line: 974 
07-17 13:14:20.143 18232-18232/com.example.game I/HomeFragment﹕ ID From AsyncTask: 112696 
07-17 13:14:20.143 18232-18232/com.example.game I/HomeFragment﹕ Adding ID to SharedPreferences! 
07-17 13:14:20.163 18232-18232/com.example.game D/HomeFragment﹕ (ON SHARED PREFERENCE CHANGE LISTENER) Setting changed: game_player_id 
07-17 13:14:20.163 18232-18232/com.example.game D/HomeFragment﹕ (ON SHARED PREFERENCE CHANGE LISTENER) Setting changed: game_player_id 
07-17 13:14:20.163 18232-18232/com.example.game D/HomeFragment﹕ (ON SHARED PREFERENCE CHANGE LISTENER) Setting changed: game_player_id 
07-17 13:14:20.163 18232-18232/com.example.game D/HomeFragment﹕ (ON SHARED PREFERENCE CHANGE LISTENER) New ID: 112696 
07-17 13:14:20.163 18232-18232/com.example.game D/HomeFragment﹕ (ON SHARED PREFERENCE CHANGE LISTENER) Setting changed: game_player_name 
07-17 13:14:20.544 18232-24361/com.example.game I/ParsePlayers﹕ Found matching ID @ line: 974 
07-17 13:14:20.544 18232-18232/com.example.game I/HomeFragment﹕ ID From AsyncTask: 112696 
07-17 13:14:20.544 18232-18232/com.example.game I/HomeFragment﹕ Adding ID to SharedPreferences! 
07-17 13:14:20.544 18232-18232/com.example.game D/HomeFragment﹕ (ON SHARED PREFERENCE CHANGE LISTENER) New ID: 112696 
07-17 13:14:20.544 18232-18232/com.example.game D/HomeFragment﹕ (ON SHARED PREFERENCE CHANGE LISTENER) Setting changed: game_player_name 
07-17 13:14:20.944 18232-24219/com.example.game D/dalvikvm﹕ GC_FOR_ALLOC freed 2503K, 30% free 18964K/26952K, paused 26ms, total 26ms 
07-17 13:14:21.034 18232-24219/com.example.game I/ParsePlayers﹕ Found matching ID @ line: 974 
07-17 13:14:21.044 18232-18232/com.example.game I/HomeFragment﹕ ID From AsyncTask: 112696 
07-17 13:14:21.044 18232-18232/com.example.game I/HomeFragment﹕ Adding ID to SharedPreferences! 
07-17 13:14:21.044 18232-18232/com.example.game D/HomeFragment﹕ (ON SHARED PREFERENCE CHANGE LISTENER) New ID: 112696 
07-17 13:14:21.044 18232-18232/com.example.game W/IInputConnectionWrapper﹕ getExtractedText on inactive InputConnection 
07-17 13:14:21.044 18232-18232/com.example.game I/HomeFragment﹕ Got this ID back: 112696 
07-17 13:14:21.044 18232-18232/com.example.game I/HomeFragment﹕ Got this ID back: 112696 
07-17 13:14:21.044 18232-18232/com.example.game I/HomeFragment﹕ Got this ID back: 112696 
07-17 13:14:21.044 18232-18232/com.example.game W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection 
07-17 13:14:21.044 18232-18232/com.example.game W/IInputConnectionWrapper﹕ getTextAfterCursor on inactive InputConnection 
07-17 13:14:21.044 18232-18232/com.example.game W/IInputConnectionWrapper﹕ getSelectedText on inactive InputConnection 
07-17 13:14:21.044 18232-18232/com.example.game W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection 
07-17 13:14:21.044 18232-18232/com.example.game W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection 
07-17 13:14:21.054 18232-18232/com.example.game W/IInputConnectionWrapper﹕ beginBatchEdit on inactive InputConnection 
07-17 13:14:21.054 18232-18232/com.example.game W/IInputConnectionWrapper﹕ finishComposingText on inactive InputConnection 
07-17 13:14:21.054 18232-18232/com.example.game W/IInputConnectionWrapper﹕ endBatchEdit on inactive InputConnection 
07-17 13:14:21.054 18232-18232/com.example.game W/IInputConnectionWrapper﹕ beginBatchEdit on inactive InputConnection 
07-17 13:14:21.054 18232-18232/com.example.game W/IInputConnectionWrapper﹕ finishComposingText on inactive InputConnection 
07-17 13:14:21.054 18232-18232/com.example.game W/IInputConnectionWrapper﹕ endBatchEdit on inactive InputConnection 
07-17 13:14:21.054 18232-18232/com.example.game W/IInputConnectionWrapper﹕ beginBatchEdit on inactive InputConnection 
07-17 13:14:21.054 18232-18232/com.example.game W/IInputConnectionWrapper﹕ finishComposingText on inactive InputConnection 
07-17 13:14:21.054 18232-18232/com.example.game W/IInputConnectionWrapper﹕ endBatchEdit on inactive InputConnection 
07-17 13:14:21.064 18232-18232/com.example.game W/IInputConnectionWrapper﹕ beginBatchEdit on inactive InputConnection 
07-17 13:14:21.064 18232-18232/com.example.game W/IInputConnectionWrapper﹕ finishComposingText on inactive InputConnection 
07-17 13:14:21.064 18232-18232/com.example.game W/IInputConnectionWrapper﹕ endBatchEdit on inactive InputConnection 
07-17 13:14:21.064 18232-18232/com.example.game W/IInputConnectionWrapper﹕ beginBatchEdit on inactive InputConnection 
07-17 13:14:21.074 18232-18232/com.example.game W/IInputConnectionWrapper﹕ finishComposingText on inactive InputConnection 
07-17 13:14:21.074 18232-18232/com.example.game W/IInputConnectionWrapper﹕ endBatchEdit on inactive InputConnection 
07-17 13:14:21.104 18232-18232/com.example.game D/AbsListView﹕ unregisterIRListener() is called 

답변

0

int tempInt = getUserIDFromAsyncTask(context, newPlayerName, newUni); 

비동기 작업의 결과를 기다리고 있습니다.

AsyncTask는 몇 초가 걸릴 수 있지만 UI 스레드를 잠그지 않으려는 짧은 작업을위한 것입니다. 미래가 실제로 필요하다면 어쨌든 작업은 AsyncTask에 너무 복잡 할 것입니다.

솔루션 :

당신은 onSharedPreferenceChanged에서 AsyncTask를 시작하고 AsyncTask.onPostResult()에 결과를 처리 할 수 ​​있습니다. onSharedPreferenceChanged 끝 부분이 아닙니다.

AsyncTask