2011-01-18 2 views
2

두 개의 서비스가 실행 중입니다. 그들은 자신의 작업을 수행 한 다음 AlarmManager를 통해 스스로 일정을 재조정합니다. BroadcastReceiver에서 일어나는 유일한 일은 Context.startService()를 통한 서비스 시작입니다. 두 서비스 모두 IntentServices이며 타임 아웃 문제가 발생하지 않아야한다고 알 수 있습니다. 나는 IntentServices, 스레딩 및 AsyncTasks를 시도했지만 수신자 자신의 시간 초과 오류에 대해 반복적으로 충돌하고 있습니다.IntentServices를 사용하는 BroadcastReceivers의 시간 초과

타임 아웃 메시지이다 01-18 11 : 29 : 04.200 : 경고/ActivityManager (73), 방송 시간 종료 BroadcastRecord 433a4168 my.package.action.a {} - [email protected] 01-18 11 : 29 : 04.210은 : 경고/ActivityManager (73) : 타임 아웃 동안 수신기 : ResolveInfo {43394a30 my.package.MyReceiverA를 p = 0 O = 0, m = 0x108000}

두 수신기의 기본 구조 :

public class MyReceiverA extends BroadcastReceiver { 
    public static final String ACTION_TO_BROADCAST = "my.package.action.a"; 
    public void onReceive(Context context, Intent intent) { 
     // start the service 
     Intent serviceIntent = new Intent().setClassName(context, 
       MyServiceA.class.getName()); 
     context.startService(serviceIntent); 
    } 
} 

그리고 서비스 :

공용 클래스 MyServiceA는 IntentService를 확장합니다. { public ActivityMonitorService() { super (TAG); 은}

public IBinder onBind(Intent intent) { 
     // We don't allow anyone to bind to us 
     return null; 
} 

public void onCreate() { 
    super.onCreate(); 
    _context = getApplicationContext(); 
    _config = new Config(); 
    if (_handler == null) { 
     _handler = new Handler(); 
    } 
} 

    /** 
    * Schedules an alarm to run ourselves again after ALARM_INTERVAL has passed. 
    */ 
    private void reschedule() { 
     Intent intent = new Intent(MyReceiverA.ACTION_TO_BROADCAST); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(_context, 0, intent, 0); 
     AlarmManager manager = (AlarmManager) _context.getSystemService(Context.ALARM_SERVICE); 
     manager.set(AlarmManager.RTC, now + delay, pendingIntent); 
    } 

private void doWork() { 
    // Do some work. This could take a while. It also accesses a database that the two 
    // services share through synchronized blocks of code in static accessor functions. 
} 

protected void onHandleIntent(Intent intent) { 
    try { 
     doWork(); 
    } catch (Exception e) { 
     // log it 
    } finally { 
     reschedule(); 
    } 
} 

}

+0

관련없는 팁 :'새로운 의도() .setClassName (context, MyServiceA.class.getName())'→'새로운 의도 (context, MyServiceA.class) ' –

+0

'서비스 코드 'Context'에 대한 참조를 ('_context'처럼) 유지하는 것과 같습니다. 'onCreate()'중에 anythin을하고 있습니까, 아니면 다른 메소드를 무시하고 있습니까? –

+0

재정의 된 함수에서 편집 됨. –

답변

1

나는 무슨 일이 있었는지 알아 냈어. 두 서비스를 하나의 서비스로 변경하면 문제가 해결되어 두 가지 서비스에 교착 상태 나 인종이 발생했습니다. 나는 그것이 그들의 데이터베이스 액세스와 함께 있다고 가정하고 있지만 그것을 아직 확인할 기회가 없었다.

단일 서비스로 변경했을 때 문제는 알람이 늦게 발사되는 것이 아니라,로드 된 전화기가 내 서비스를 일시 중지하여 음악 플레이어에 필요한 리소스를 제공한다는 것입니다. 내 옵션처럼 그것과 함께 살거나 포 그라운드에서 서비스를 실행하는 것 같습니다.