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.

7
  • Is your app on SD card ? Commented Dec 12, 2013 at 18:56
  • I tried this on the emulator, and its not working, so i didn't made this step already. Commented Dec 12, 2013 at 18:56
  • Try it on your phone, and let us know if it works. Commented Dec 12, 2013 at 19:00
  • Also try putting the full package name on your receiver name Commented Dec 12, 2013 at 19:01
  • i added the full package name, and i tried on the device, not working Commented Dec 12, 2013 at 20:07

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

What happens when you issue the emulator command above? Is your BroadcastReceiver code called?
yes, my broadcastreceiver code is called, but when restarting the phone it doesnt
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.

Comments

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.

1 Comment

i have done what you told me and also tested on my phone, and its not working
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

2 Comments

Is your BootReceiver class in a sub package? If not, try moving it to the root. Also, what phone are you testing this with?
i have it in the root, i test with a galaxy s2+
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.