views:

104

answers:

1

I am unable to get my BroadcastReceiver onReceive method called using the BOOT_COMPLETED intent.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.jerrellmardis.umbrella"
      android:versionCode="4"
      android:versionName="1.0.3">
    <application android:icon="@drawable/icon" android:label="@string/app_name" 
            android:theme="@android:style/Theme.NoTitleBar">
        <activity android:name=".activities.Umbrella" android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".activities.Preferences" android:label="@string/app_name" android:screenOrientation="portrait" />
        <receiver android:name="com.jerrellmardis.umbrella.receiver.WeatherStartupReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <service android:name=".service.WeatherUpdateService">
            <intent-filter>
                <action android:name="com.jerrellmardis.umbrella.service.WeatherUpdateService" />
            </intent-filter>
        </service>
    </application>
    <uses-sdk android:minSdkVersion="3" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>

WeatherStartupReceiver.java

package com.jerrellmardis.umbrella.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Contacts.People;
import android.util.Log;

import com.jerrellmardis.umbrella.R;

public class WeatherStartupReceiver extends BroadcastReceiver {

       private NotificationManager mNotificationManager;
       private int SIMPLE_NOTFICATION_ID;

       @Override
       public void onReceive(Context context, Intent intent) {
                // Do something interesting here...
       }
}
A: 

EDIT: Forget everything, I've found a better explanation.

You have to define your receiver with exported = true and enabled = true

<receiver android:name="com.jerrellmardis.umbrella.receiver.WeatherStartupReceiver"
  android:enabled="true" 
  android:exported="true" 
>

I think that if you change this line

<receiver android:name="com.jerrellmardis.umbrella.receiver.WeatherStartupReceiver">

for this

<receiver android:name=".WeatherStartupReceiver">

it will fix your problem.

I tried it on one of my projects and it didn't start.

brent
I don't know why you've been having problems, but I certainly haven't had to declare enabled and exported for my BOOT_COMPLETED receivers to receive notifications.
beekeeper
I've reread the android documentation about this, and certainly they say that both enabled and exported properties are set by default "true"... Again I'm wrong :S. I don't see anything else strange on oracleicom's code
brent
So change the name of the receiever to ".WeatherStartupReceiver" even though the class is not at the root of my app dir? The current directory structure is <APP_HOME>/receiver/WeatherStartupReceiver.java
oracleicom
So I tried changing the name of the receiver in the AndroidManifest but that didn't work. Just to make sure I wasn't noticing that the method was being called, I changed the receiver's onReceive method to throw an exception and still nothing.
oracleicom
Try another thing, remove the R import. R is resolved automatically.
brent
Actually I'm just throwing an exception right now so the isn't an R import in the code. And it's still not working :-(
oracleicom
SOLVED!!! It wasn't working because the app was being installed on the SD card, thus missing the BOOT_COMPLETED intent. THANKS FOR ALL YOUR HELP EVERYONE!
oracleicom