2013-08-20 1 views
4

Android 프로젝트를 진행하고 있으며 다음 코드를 향상시킬 수있는 방법을 찾으려고합니다. 나는이 개발 한 방법은 OK인지 아닌지 내가 알 필요가 :IntentService를 여러 번 시작하십시오. android

  • 통지는 DB에서 자궁강 된 GCM 알림입니다 내 질문에 내가 몇 가지를 호출하고 의도 서비스에 관한

타임스. 괜찮 니? 어떻게 개선해야합니까?

while (((notification)) != null) 
{{ 
    message = notification.getNotificationMessage(); 
    //tokenize message 
    if (/*message 1*/) { 
     Intent intent1= new Intent(getApplicationContext(),A.class); 
     intent1.putExtra("message1",true); 
     startService(intent1); 
    } else 
    { 
     Intent intent2= new Intent(getApplicationContext(),A.class); 
     intent2.putExtra("message2",true); 
     startService(intent2); 
    } 
} 
//retrieve next notification and delete the current one 
} 

답변

6

IntentService는 비동기 적으로 작업자 스레드에서 실행되도록 사용됩니다. 필요한 경우 여러 번 호출하는 것은 잘못입니다. 인 텐트는 하나의 스레드에서 하나씩 실행됩니다.

IntentService에서 파생 된 클래스가 있고 그 onHandleIntent()가 재정의 된 것으로 가정합니다. 개선 만하면 두 개의 별도 의도를 만들 필요가 없다는 것을 알 수 있습니다. 기본적으로 서로 다른 추가 기능과 동일한 의도입니다. onHandleIntent()에서이 두 가지를 구분할 수 있습니다.

while (notification != null) 
{ 
    Intent intent= new Intent(getApplicationContext(),A.class); 
    message = notification.getNotificationMessage(); 

    if (msg1 condition) { 
     intent.putExtra("message1",true); 
    } else { 
     intent.putExtra("message2",true); 
    } 

    startService(intent); 

    //retrieve next notification and delete the current one 
} 
:

그래서 코드가 같아야합니다
관련 문제