0

My broadcastreceiver doesnt work, i dont get the message in the log, can you please help me? This is my broadcastreceiver:

public class BootReceiver extends BroadcastReceiver{
    public static SharedPreferences prefs;

    @Override
    public void onReceive(Context context, Intent intent) {
        prefs = PreferenceManager.getDefaultSharedPreferences(context);
        // TODO Auto-generated method stub
        Log.w("A intrat in BootReceiver"," ");
        if (!(prefs.getString(NotificareOptions.OptionsPos, "2")).equals("1"))
        context.startService(new Intent(context, ServiceNotif.class));

    }

}

i got the permission, and i have the receiver declared in the manifest.

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

 <receiver android:name=".BootReceiver"  
            android:enabled="true"
            android:exported="true" >
            <intent-filter android:priority="500" >
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

Where is the problem? I also heard that RECEIVE_BOOT_COMPLETED doesnt work on all android phones.

victorholo
  • 105
  • 1
  • 11

5 Answers5

2

Your code looks correct. This may be an issue with the emulator. I too had problems with the BOOT_COMPLETE broadcast on the emulator. It did not always fire when re-starting the emulator. The following ADB command did the trick however:

adb.exe -s emulator-5554 shell
# am broadcast -a android.intent.action.BOOT_COMPLETED

This will cause the emulator to broadcast a BOOT_COMPLETED message, just as a real device does at startup. This also has the advantage of being much faster than re-starting the emulator.

EJK
  • 12,332
  • 3
  • 38
  • 55
1

After i replaced android:exported="true" with android:exported="false" in my manifest receiver it started working. In many tutorials it doesn't say about it, but maybe this could help someone else too.

victorholo
  • 105
  • 1
  • 11
0

A couple of things I notice. In your BootReceiver you specify:

<receiver android:name=".BootReceiver"

This implies that the receiver is in the default package (e.g. in your manifest the value of package). If this is not the case, or if you are unsure, you can specify the full package like this:

<receiver android:name="com.mycompany.boot.BootReceiver"

Also, it's possible that your log message is working but you cannot see it. You log as:

Log.w("A intrat in BootReceiver"," ");

So the tag is "A intrat in BootReceiver" and the message is empty. Try putting in a message value that isn't empty.

Also, you can see that if Intent was fired or not if you have adb logcat running when the device reboots.

David S.
  • 6,567
  • 1
  • 25
  • 45
0

I can't see anything directly wrong. But a couple of things to try:

  1. Try naming your receiver with its full package.name.path.BootReceiver
  2. Remove enabled, exported and priority from the receiver declaration
Mike
  • 1,000
  • 1
  • 10
  • 18
0

Based on your indication that changing the value of android:exported resolved your issue, I think something else might have been in play -- rebuilding your code from scratch.

Conducting a 'Clean' in Eclipse (or the equivalent command in your development environment) could resolve this problem for others in the future.

mike47
  • 2,217
  • 1
  • 27
  • 50