2012-11-16 6 views
0

다음 튜토리얼을 통해 GCM을 배우고 있습니다 : Android Developer Guide. "응용 프로그램이 서비스를 시작하기 전에 잠자기를 알아야합니다. 그렇지 않으면 서비스가 시작되기 전에 잠자기 상태가 될 수 있습니다."
수신자는 먼저 메시지를 수신 한 다음 IntentService의 자체 구현, 코드는 다음과 같습니다.Google 클라우드 메시징의 WakeLock

내 질문에 WakeLockIntentService 클래스에 있지만 Receiver 클래스에는없는 이유는 무엇입니까?

public class MyBroadcastReceiver extends BroadcastReceiver { 

    @Override 
    public final void onReceive(Context context, Intent intent) { 
     MyIntentService.runIntentInService(context, intent); 
     setResult(Activity.RESULT_OK, null, null); 
    } 
} 




public class MyIntentService extends IntentService { 

    private static PowerManager.WakeLock sWakeLock; 
    private static final Object LOCK = MyIntentService.class; 

    static void runIntentInService(Context context, Intent intent) { 
     synchronized(LOCK) { 
      if (sWakeLock == null) { 
       PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
       sWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "my_wakelock"); 
      } 
     } 
     sWakeLock.acquire(); 
     intent.setClassName(context, MyIntentService.class.getName()); 
     context.startService(intent); 
    } 

    @Override 
    public final void onHandleIntent(Intent intent) { 
     try { 
      String action = intent.getAction(); 
      if (action.equals("com.google.android.c2dm.intent.REGISTRATION")) { 
       handleRegistration(intent); 
      } else if (action.equals("com.google.android.c2dm.intent.RECEIVE")) { 
       handleMessage(intent); 
      } 
     } finally { 
      synchronized(LOCK) { 
       sWakeLock.release(); 
      } 
     } 
    } 
} 

답변

1

wakock은 서비스 용입니다. wakelock 객체로 작업을 마쳤 으면 객체를 릴리스해야합니다.