Monday, December 24, 2012

Android BroadcastReceiver is not working after install ? How to fix this ?

Starting from Android 3.1 after you install an application all broadcast receivers are in "STOPPED" state until you click on the icon and start the application. I wanted dig-in to find a solution for this problem. This is what I discovered under the hood.

 com.android.server.pm.Settings.readStoppedLPw() is invoked on BOOT_COMPLETE and it checks for


String tagName = parser.getName();
if (tagName.equals("pkg")) {
    String name = parser.getAttributeValue(null, "name");
    PackageSetting ps = mPackages.get(name);
    if (ps != null) {
        ps.stopped = true;
        if ("1".equals(parser.getAttributeValue(null, "nl"))) {
            ps.notLaunched = true;
        }
    } else {
        Slog.w(PackageManagerService.TAG, "No package known for stopped package: " + name);
    }
so, what is this "nl" ? When you install an apk, your broadcast receiver will listed on /data/system/packages-stopped.xml 


<stopped-packages>
<pkg name="test.broadcast" nl="1" />
</stopped-packages>

So, If you want to enable your packages back, you must modify this file and push it back to /data/system/packages-stopped.xml  with nl="0" 

to get it to work.

and then rebroadcast the BOOT_COMPLETED and you should be fine

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

to do all these you must have root. otherwise no choice