Alexander Trauzzi
Alexander Trauzzi

Reputation: 7405

BroadcastReceiver not being notified of BOOT_COMPLETED

I'm trying to rig together a service that simply issues a notification on "boot" and then another one once the service is running. I'm using the following for my manifest:

<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.project"
    android:versionCode="1"
    android:versionName="1.0"
>

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17"
    />

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
    >

        <service
            android:name=".Manager"
        />

        <receiver
            android:name=".Bootstrap"
        >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter> 
        </receiver>

    </application>

</manifest>

Unfortunately when I reboot my device, nothing displays and it seems like the service is never started.

Here is the implementation for my BroadcastReceiver subclass:

    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("Test", "Bootstrap run!");

        int notificationId = 1;
        NotificationCompat.Builder startNotification = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Glance")
            .setContentText("Starting the Glance service.")
        ;
        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(notificationId, startNotification.build());

        //
        // Run the service.
        //
        Intent startServiceIntent = new Intent(context, Manager.class);
        context.startService(startServiceIntent);

    }

Any suggestions on what I might be doing wrong or missing here that's causing things not to work?

Thanks!

Upvotes: 1

Views: 855

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007554

If you are testing this on Android 3.1, please add an activity to your application, then run your activity. Starting with Android 3.1, manifest-registered BroadcastReceivers are blocked until something manually runs one of your components, which typically means a user has launched one of your activities. See this nearly-two-year-old blog post for more details.

Upvotes: 3

Related Questions