使用IntentServices的BroadcastReceivers超时

时间:2011-01-18 18:09:13

标签: android

我有两个服务正在运行。他们完成工作,然后通过AlarmManager重新安排自己。在BroadcastReceiver中,唯一发生的事情是通过Context.startService()启动服务。这两个服务都是IntentServices,据我所知,这不应该导致超时问题。我已经尝试过IntentServices,threading和AsyncTasks,但我反复碰到接收器本身的超时错误。

超时消息是: 01-18 11:36:04.200:WARN / ActivityManager(73):广播超时BroadcastRecord {433a4168 my.package.action.a} - receiver=android.os.BinderProxy@43399978 01-18 11:29:04.210:WARN / 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(){             超级(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();
    }
}

}

1 个答案:

答案 0 :(得分:1)

我弄清楚发生了什么事。将两个服务更改为单个服务可以解决问题,这意味着两者中存在某种僵局或竞争。我假设这是他们的数据库访问,但还没有机会验证它。

当改为单一服务时,问题不在于闹钟响起,而是下载的手机正在暂停我的服务,为音乐播放器提供所需的资源。看起来我的选择是使用它或在前台运行服务。