简体   繁体   中英

What's the right way to send messages from my IntentService back to an Activity?

I have an app in which the main Activity starts an AlarmReceiver that calls an IntentService that runs in the background and does stuff. I'm unclear on what the correct way is to check on the IntentService's actions and present the end-user with some feedback in the visible Activity that they're in, on the IntentService's current state. In my ideal world there can be an icon somewhere on the screen that I can set to notify the user of what's going on with the IntentService. I don't need the user to be able to *do anything, just have feedback.

All advice welcome.

Android has a notification API, which is even easy to use - Creating Status Bar Notifications .

If you want your activity to receive updates from the service, I would suggest using broadcasts and broadcast receivers .

How to send a broadcast intent:

Intent i = new Intent("your.action");
sendBroadcast(i);

To receive this broadcast within your activity, you have to implement a broadcast receiver:

private BroadcastReceiver myReceiver = new BroadcastReceiver() {        
    @Override
    public void onReceive(Context context, Intent intent) {
        //
    }
};

which you have to register...

registerReceiver(myReceiver, new IntentFilter("your.action"));

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