简体   繁体   中英

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;

@Override
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);
        context.sendBroadcast(i);

    } 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() {
    @Override
    public void onReceive(Context context, Intent intent) {

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

        ApplicationInfo appInfo = (ApplicationInfo) applicationInfo;

        getNewData(appInfo);
    }
};

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

@Override
public void onStop()
{

    try{

        unregisterReceiver(broadcastReceiver);
    }

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

    super.onStop();
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM