开发者

Timeout in BroadcastReceivers using IntentServices

I've got two services running. They do their work and then reschedule themselves via the AlarmManager. In the BroadcastReceiver the only thing that happens is the launching of the service via Context.startService(). Both services are IntentServices, which as far as I can tell shouldn't be causing timeout problems. I've tried IntentServices, threading, and AsyncTasks but am repeated开发者_如何学JAVAly bumping up against the timeout error in the receivers themselves.

The timeout message is: 01-18 11:29:04.200: WARN/ActivityManager(73): Timeout of broadcast BroadcastRecord{433a4168 my.package.action.a} - receiver=android.os.BinderProxy@43399978 01-18 11:29:04.210: WARN/ActivityManager(73): Receiver during timeout: ResolveInfo{43394a30 my.package.MyReceiverA p=0 o=0 m=0x108000}

The basic structure of the two receivers:

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);
    }
}

And the services:

public class MyServiceA extends 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();
    }
}

}


I figured out what was going on. Changing the two services to be a single one fixed the problem, meaning there was some sort of deadlock or race going on in the two. I'm assuming it's with their database access but haven't had a chance to verify it yet.

When changing to a single service the problem wasn't that the alarm was firing late, it's that the loaded down phone was pausing my service to give the music player the resources it needed. Looks like my options are to live with it or run the service in the foreground.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜