什么是正确的方法,从我的IntentService将消息发送回一个活动? [英] What's the right way to send messages from my IntentService back to an Activity?

查看:195
本文介绍了什么是正确的方法,从我的IntentService将消息发送回一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序中的主要活动开始调用运行在后台做的东西的IntentService的AlarmReceiver。我不清楚正确的方法是检查在IntentService的行动和present最终用户在可见的活动,他们是在一些反馈,对IntentService的当前状态是什么。在我的理想世界可以有,我可以设置为通知什么的IntentService会在用户屏幕上的某个图标。我不需要用户能够*做任何事情,只是有反馈。

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.

所有建议欢迎。

推荐答案

Android有一个通知API,它甚至易于使用 - 的创建状态栏通知

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.

如何发送广播意图:

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

这篇关于什么是正确的方法,从我的IntentService将消息发送回一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆