Skip to content Skip to sidebar Skip to footer

Why BroadcastReceiver Is Not Running In The Background?

In one application (App1) I am broadcasting a message. This code below is correct -> the broadcast is detected if I try to get Broadcast in the same project. sendBroadcast(new I

Solution 1:

v6 - Sample code on github

In case it helps, I put together a very quick demo app (for Eclipse) which contains a single exported, insecure BroadcastReceiver which you can use as a template. I have stored it on my github account:

You can use it to follow my advice given in v3 of my answer, step 2, where I suggested you get a simple exported BroadcastReceiver working.

I am sure the rest of the process will be quite simple once you have this up and running.


v5 - Manifest receiver as separate class

You can declare a BroadcastReceiver in two different ways:

  • as a field in another class (as in your MainActivity)
  • or as a separate class

When declared in another class, you need to use the methods ContextWrapper.registerReceiver() and ContextWrapper.unregisterReceiver() to register for intents. Your code needs to do this, so your app must be running in order to receive the broadcast.

If you want to "wake up" your app with a broadcast, that is when you declare the receiver in the manifest (like you did). In that case, your BroadcastReceiver will be a separate class file:

package mobiric.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * Must be declared in the manifest.
 */
public class MobiricReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        // do something
    }
}

It looks like you are mixing up these 2 techniques. I think you need the 2nd technique:

  • declare in manifest
  • create separate class file based on code

v4 - Manually launch both apps

The last thing I can think of at the moment is that I think you need to manually launch your app before the system allows a BroadcastReceiver to work.

So be sure to launch any Activity in your app before testing.

Here's a blog post about that:


v3 - Grant your app the Custom Permission

Oh wait - you haven't declared <uses-permission android:name="com.examples.my_permissions.MY_PERMISSION" /> in your manifest.

But I would normally expect logcat to tell you that you have a permission issue.

For what it's worth, I would break this issue down into a couple of steps:

  1. remove all required custom permissions from both apps
  2. debug your broadcast receiver so that it works in this insecure configuration
  3. finally add the custom permissions

That way you isolate potential problems so you are only ever dealing with one tricky issue at a time. It will make it easier to focus on the problem without having other possibilities to track down.

In addition, by removing the custom permissions at development time, it enables you to use ADB to test your receiver, which can save a lot of time. Look for details on the adb shell am broadcast ... here:


v2 - Sign your apps to use Custom Permissions

Original question updated with exported="true".

Using custom permissions requires both apps to be signed, using the same signing key.

However I have run into unsolved "custom permission" issues in the past. Basically my new custom permissions were not being granted on the device. Uninstalling and reinstalling the app did NOT make any difference - it seemed that the system stored that custom permission in a place that I could not update it.

Here is the related post on this forum - unfortunately I was never able to solve the issue, and had to live with the provided workaround:


v1 - Export the BroadcastReceiver

You need to "export" your BroadcastReceiver by putting exported="true" in the manifest declaration of the receiver.

See change below:

    <receiver
        android:name="MyReceiver"
        android:permission="com.examples.my_permissions.MY_PERMISSION"
        android:exported="true" >

        <intent-filter>
            <action android:name="com.example.MESSAGE_INTENT" />
        </intent-filter>
    </receiver>

Note: future readers of this answer, please note that the original post included a permission. This prevent security issues, and requires that the calling app is signed with the same signature.


Post a Comment for "Why BroadcastReceiver Is Not Running In The Background?"