StartupReceiver::onReceive() NOT Called at Boot


#1

Hello,

I’ve tested the code in this chapter on an Android 4.3 device (a nexus 4), and I do see the onReceive() method called, and everything works as expected and explained in the chapter. So I believe I haven’t missed anything while following along in the book.

When I test the exact same project with an Android 4.0.4 device(a motorola xoom) I dont see the method getting called. Any ideas on how to go about debugging something like this?

Btw - really excellent book, thank you! I’ve enjoyed working through it.

Thanks,
-yns


#2

Howdy. Thanks for the kind words about the book. Let’s see if we can figure out this issue.

First of all, you might want to post your AndroidManifest.xml file in reply here, so we can be sure everything is in just the right spot/format. Specifically:

  • the uses-permission for RECEIVE_BOOT_COMPLETED should be a direct child of the manifest element
  • the “name” attribute of the receiver element should be the fully-qualified package+class

You can test the boot completed intent without rebooting anything using adb to send the intent, as described here:

stackoverflow.com/a/9257243/209022

Hopefully this gets you on the path to a solution…


#3

Thank you for the reply. It now works now on the 4.0.4 device, even with the original manifest (posted below). I am unsure what was/is going on. Here is what I tried:

  1. The adb shell method doesnt seem to trigger the log message (neither on the 4.3 device nor the 4.0.4). Not sure why, any ideas?
  2. In the manifest I changed android:name=".StartupReceiver" to android:name=“com.bignerdranch.android.photogallery.StartupReceiver”. After this I started seeing the log message.
  3. I couldn’t really make sense of this, because the above change should be a no-op. So I reverted it, and it still works now. The only other thing I remember doing is that I uninstalled the PhotoGallery app after making the manifest change in step 2, and before doing “Run As…” from eclipse. Something during this process seems to have realigned the misaligned stars. I usually do not do the uninstall step.

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bignerdranch.android.photogallery"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" />
    
    <permission android:name="com.bignerdranch.android.photogallery.PRIVATE"
        android:protectionLevel="signature" />    
    
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="com.bignerdranch.android.photogallery.PRIVATE" />
    
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
        <activity
            android:name=".PhotoGalleryActivity"
            android:launchMode="singleTop"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data android:name="android.app.searchable"
                 android:resource="@xml/searchable"/>            
        </activity>
        
        <service android:name=".PollService" />
    
        <receiver android:name=".StartupReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <receiver android:name=".NotificationReceiver"
            android:exported="false">
            <intent-filter android:priority="-999">
                <action android:name="com.bignerdranch.android.photogallery.SHOW_NOTIFICATION" />
            </intent-filter>
        </receiver>
        
    </application>

</manifest>

The adb shell command returned the following:

$ adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n com.bignerdranch.android.photogallery/StartupReceiver
Broadcasting: Intent { act=android.intent.action.BOOT_COMPLETED cat=[android.intent.category.HOME] cmp=com.bignerdranch.android.photogallery/StartupReceiver }
Broadcast completed: result=0

#4

Glad it’s working for you now.

For the adb shell command, you need to specify both the package and fully-qualified class name for that to work (to my knowledge). So it should be like this:


#5

Ah, thanks. Somehow missed the fully-qualified classname. That did the trick.

Thanks again,
-yns