简体   繁体   English

将消息从我的IntentService发送回活动的正确方法是什么?

[英]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. 我有一个应用程序,其中主Activity启动一个AlarmReceiver,它调用在后台运行的IntentService并执行操作。 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. 我不清楚检查IntentService的操作的正确方法是什么,并在IntentService的当前状态下向最终用户提供他们所在的可见活动中的一些反馈。 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. 在我的理想世界中,屏幕上可以有一个图标,我可以将其设置为通知用户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 . Android有一个通知API,它甚至易于使用 - 创建状态栏通知

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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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