从服务访问findViewById [英] Accessing findViewById from Service

查看:85
本文介绍了从服务访问findViewById的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要访问 findViewById()从服务没有传递给它作为服务的构造函数的参数或意图,这可能吗?

I want to Access findViewById() from an Service without passing it as an parameter or intent in the constructor of the service, Is that possible?

她我的code:

public class ManageDataService extends Service implements LocationListener {
    public void onCreate() {
        TextView longitudeField = (TextView) findViewById(R.id.overview_position_longitude);
    }
}

我必须以某种方式得到了活动的背景下,对吧?

I have to get the Activity's context somehow, right?

我没有在互联网上找到任何匹配的解决方案。

I didn't find any matching solutions on the internet.

推荐答案

这是非常糟糕的主意更新UI形式的服务。
我想好了办法,以它BroadcastReceivers观测数据的使用。
只需注册一个用于您的活动,并通过价值来更新UI。

It's really bad idea to update UI form service. I think good approach to to it is usage of BroadcastReceivers data Observer. Just register one for your activity and pass value to update UI.

这篇关于从服务访问findViewById的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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