Natalie Natalie - 1 year ago 411
Android Question

Send data from BroadcastReceiver to activity, while app is in background

I am writing my code in Android Studio, but the project that I am writing has no GUI, and never updates views for a user. It simply listens for the last installed app with a Broadcast Receiver and sends info to another java file.

I am wondering how I can send a string from a broadcast receiver to an activity (maybe this should not be an "activity" but a "service"?), and have this work while the app is in the background.

Right now I have a broadcast receiver listening for the last installed app on my phone (this runs in the background). And I have a custom broadcast receiver set up to notify my main activity when the broadcast receiver gets an installed app. But this custom receiver doesn't work in the background. Again, maybe I shouldn't be using an Activity to receive info from the BroadcastReceiver?

Is there a way I can send information from my broadcast receiver to my main activity while the app is in the background? I also need my main activity to resume normal function while in the background (there are no GUI updates done while it's in the background).

My BroadcastReceiver which is sending data to my Main Activity through a new Intent

public class NewInstallReceiver extends BroadcastReceiver {

ApplicationInfo ai;

public void onReceive(Context context, Intent intent) {

final PackageManager pm = context.getPackageManager();

try {

// if app is added, get package info

ai = pm.getApplicationInfo(intent.getData().getSchemeSpecificPart(), 0);

Intent i = new Intent("broadcastName");
// Data pass to activity
i.putExtra("appInfo", ai);

} catch (final PackageManager.NameNotFoundException e) {
ai = null;


In my MainActivity in onCreate I register the receiver:

registerReceiver(broadcastReceiver, new IntentFilter("broadcastName"));

I receive data from BroadcastReceiver

BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {

// get applicationInfo
Bundle b = intent.getExtras();
Object applicationInfo = b.get("appInfo");

ApplicationInfo appInfo = (ApplicationInfo) applicationInfo;


Then I have to unregister the receiver in MainActivity, but obviously this doesn't allow for me to receive info from BroadcastReceiver once the app goes in the background

public void onStop()



catch(Exception e){
System.out.println("Exception is " + e.toString());


Answer Source

Honestly, I don't think that update an Activity that is in background is a good practice (even if possible).

According to DOCS:

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). While activities are often presented to the user as full-screen windows, they can also be used in other ways: as floating windows... or embedded inside of another activity (using ActivityGroup).

So, if the Activity is in background, it does not visible. So, it is useless to update its contents (because it is not visible to user).

In fact, you should implement following behavior instead:

  • Use your Service only to save the information (in a database/content provider) and to notifies the Activity that a new information is available. Then, if your activity is open, it immediately consumes that information. If Activity is not opened, the info remains saved for when the user needed it.

  • If app was only stopped, you can update the Activity content during onResume() method.

  • If your app was killed and user opened it again, you can update ALL Views during onCreate() method. onCreate() is called before the View is displayed to user.

This way, the info will be updated when needed: when the user wan's to check the info. Otherwise, it is a waste of time.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download