2011-04-08

Why TaskKiller prevents my Alarm apps from working

It is well-known that task killers on the Android Market prevents Alarm applications from working. But why? To understand this, we should understand how alarms in Android are handled. A hardware alarm event should go through 2 layers to bring up your software component.
  1. Hardware alarm event
  2. AlarmManagerService
  3. Your application components to handle alarms
The AlarmManagerService stores the information required to link hardware alarm event to your application components. The task killer actutually removes these information from AlarmManagerService and this is why your Alarm application doesn't work after a task killer kills your application.

For devices running Android 2.1 and below, the method the task killer uses to kill an application is ActivityManager.restartPackage(String) that in turn triggers the UninstallReceiver defined in AlarmManagerService to remove all links registered by your application.
class UninstallReceiver extends BroadcastReceiver {
        public UninstallReceiver() {
            IntentFilter filter = new IntentFilter();
            filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
            filter.addAction(Intent.ACTION_PACKAGE_RESTARTED); // <------ this line
            filter.addDataScheme("package");
            mContext.registerReceiver(this, filter);
        }
        
        @Override
        public void onReceive(Context context, Intent intent) {
            synchronized (mLock) {
                Uri data = intent.getData();
                if (data != null) {
                    String pkg = data.getSchemeSpecificPart();
                    removeLocked(pkg);
                    mBroadcastStats.remove(pkg);
                }
            }
        }
    }

But, in Android 2.2, things changed. The restartPackage method is changed to be just a wrapper of a new method killBackgroundProcesses. So, task killers on the Market are doing the same thing as default Out-Of-Memory (OOM) killer. Although we can still install them to proactively kill processes, but they are not necessary any more.