Android服务如何更新启动它的活动的UI? [英] How can Android service update the UI of the activity that started it?

查看:92
本文介绍了Android服务如何更新启动它的活动的UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android编程的新手,因此在选择最佳的应用程序设计方式时遇到了一些普遍的问题.

I'm new to Android programming, so I'm facing some general problems about choosing the best way to design my app.

我想做的基本上是一个媒体播放器.我希望媒体播放器在服务上运行,因为我希望它在不显示活动时也能播放.

What I want to do is basically a media player. I want the media player to run on a service because I want it to play also when the activity is not displayed.

我的问题是,如何根据服务工作流程(例如,歌曲更改并希望显示其名称)来更新活动上的UI?

My question is, how can I update the UI on my activity depending on the service working flow (for example, the song changes and I want its name to be displayed)?

我想我可以使用本地广播管理器来将服务中的意图发送到我的活动并调用UI更新(看起来正确吗?)

I guess I could use a Local Broadcast Manager in order to send intents from my service to my activity and invoke UI updates (does it seem right?)

但是...我希望我的服务在播放音乐时做一些事情(例如查询/更新数据库). 因此,我正在考虑在其他进程(或线程?)上运行服务.

BUT... I will want my service to do some stuff while playing music (like querying/updating the DB). For this reason I was thinking on running the service on a different process (or thread?).

SO ..我想,在不同的进程上运行服务,我将无法使用本地广播管理器(对吗?).

SO.. I guess, running service on a different process, I won't be able to use local broadcast manager (is this right?).

我希望我能解释我的疑问...任何人都可以提供帮助?

I hope I explained what are my doubts... anyone can help?

非常感谢!

推荐答案

在服务中使用异步任务来处理您需要在后台完成的工作.当您需要更新UI时,请使用异步任务的progressUpdate方法将广播发送回任何感兴趣的活动.

Use an async task in your service to handle the work you need done in the background. When you need to update the UI use the progressUpdate method of async task to send a broadcast back to any interested activities.

伪示例.

活动

onCreate-> startService并创建新的broadcastReceiver.确保覆盖onReceive方法并测试特定的意图.

onCreate -> startService and create new broadcastReceiver. Make sure to override the onReceive method and test for the specific intent.

    mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);

    broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(yourActionType)) {
                //do work here
            } 
        }
    };

onResume->注册为广播接收器

onResume -> register as a broadcast receiver

    IntentFilter filter = new IntentFilter();
    filter.addAction(yourActionType);
    mLocalBroadcastManager.registerReceiver(broadcastReceiver, filter);

服务

onCreate->创建广播管理器.

onCreate -> create broadcast manager.

   mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);

onStartCommand->必要时创建并执行新的异步任务. (onStart可以被多次调用)

onStartCommand -> create and execute a new async task if necessary. (onStart could be called multiple times)

异步任务

doInBackground->启动所需的任何后台任务.在这种情况下,播放音乐. 定期调用publishProgress

doInBackground -> Start whatever background task you need. In this case playing music. Make periodic calls to publishProgress

onProgressUpdate-> sendBroadcast指示更新状态

onProgressUpdate -> sendBroadcast indicating updated status

    Intent broadcastIntent = new Intent(yourActionType);
    broadcastIntent.putExtra(whateverExtraData you need to pass back);
    mLocalBroadcastManager.sendBroadcast(broadcastIntent);

onPostExecute-> sendBroadcast表示任务已结束

onPostExecute -> sendBroadcast indicating task ended

这篇关于Android服务如何更新启动它的活动的UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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