2013-07-03 2 views
1

내 응용 프로그램은 SyncAdapter를 사용하여 서버 데이터를 SQLite와 주기적으로 동기화합니다. 또한 새/업데이트 된 서버 데이터를 나타내는 GCM 메시지에 대한 응답으로이 데이터를 동기화합니다. 통하다. IntentService.동일한 응용 프로그램 내에서 SyncAdapter 및 GCM을 조정하는 모범 사례

이러한 구성 요소는 서로 다른 백그라운드 스레드에서 각각의 작업을 수행하며 서로 다른 시스템 프로세스 (SyncManager/GCM 브로드 캐스트)에서 생성되며 수명주기가 다릅니다. 예기치 않게!

이러한 구성 요소를 내결함성이있는 방식으로 조정/제어하는 ​​방법에 대한 아이디어를 요청하고 있습니다 (예 : 활동을 통해 작업하지 않아도된다는 신호). 예 : GCM IntentService가 작동 중일 때 SyncAdapter에 신호를 보내 작업하지 않습니다.

답변

3

당신이 대신 IntentService의 SyncAdapter를 시작 GcmBroadcastReceiver에서 IntentService

  • 을 제거 SyncAdapter
  • 로 모든 동기화 코드를

    1. 을 넣어해야합니다.

    다음은 SyncAdapter documentation에서 복사 된 예제 코드입니다.

    public class GcmBroadcastReceiver extends BroadcastReceiver { 
        ... 
        // Constants 
        // Content provider authority 
        public static final String AUTHORITY = "com.example.android.datasync.provider" 
        // Account type 
        public static final String ACCOUNT_TYPE = "com.example.android.datasync"; 
        // Account 
        public static final String ACCOUNT = "default_account"; 
        // Incoming Intent key for extended data 
        public static final String KEY_SYNC_REQUEST = 
         "com.example.android.datasync.KEY_SYNC_REQUEST"; 
        ... 
        @Override 
        public void onReceive(Context context, Intent intent) { 
         // Get a GCM object instance 
         GoogleCloudMessaging gcm = 
          GoogleCloudMessaging.getInstance(context); 
          // Get the type of GCM message 
         String messageType = gcm.getMessageType(intent); 
         /* 
         * Test the message type and examine the message contents. 
         * Since GCM is a general-purpose messaging system, you 
         * may receive normal messages that don't require a sync 
         * adapter run. 
         * The following code tests for a a boolean flag indicating 
         * that the message is requesting a transfer from the device. 
         */ 
         if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType) 
          && 
          intent.getBooleanExtra(KEY_SYNC_REQUEST)) { 
          /* 
          * Signal the framework to run your sync adapter. Assume that 
          * app initialization has already created the account. 
          */ 
          ContentResolver.requestSync(ACCOUNT, AUTHORITY, null); 
          ... 
         } 
         ... 
        } 
        ... 
    }