更新的TextView与本地服务的活动 [英] Updating Textview in an Activity with Local Service

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

问题描述

我有一个活性的R.id.eventDistance和R.id.eventTime显示的距离和放大器;根据当前的位置旅行时间。我计算出使用我的位置服务类每30秒这些值。

I have an Activity with R.id.eventDistance and R.id.eventTime to display the distance & travel time based on current location. I calculate these values every 30 seconds using my Location Service class.

我的问题是:我怎么更新活动TextView的?我试着找了这个问题,并发现了一些可能的解决方案,如使用一个BroadcastReceiver,并呼吁TextView.setText()中的onReceive()方法。我不知道如何做到这一点。我应该传递这样的活动类:

My question is: how do I update the TextView in Activity? I've tried looking up the question and found some possible solutions such as using a BroadcastReceiver and call TextView.setText() in the onReceive() method. I'm not sure how this is done. Am I supposed to pass in the Activity class like this:

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Intent i = new Intent(context, HomeActivity.class);
   }
} 

我不知道以后的事。任何帮助是AP preciated。

I'm not sure what to do after that. Any help is appreciated.

推荐答案

请参阅该流程。

1.in你LocationService类时要更新的用户界面。在你的情况下,它应该被称为每隔30秒。

1.in your LocationService class when you want to update UI. in your case it should be called every 30 seconds.

Intent i = new Intent("LOCATION_UPDATED");
i.putExtra("<Key>","text");

sendBroadcast(i);

2.in你的UI(HomeActivity)班

2.in your UI (HomeActivity) class

在的onCreate()

in onCreate()

registerReceiver(uiUpdated, new IntentFilter("LOCATION_UPDATED"));

private BroadcastReceiver uiUpdated= new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        TextView.setText(intent.getExtras().getString("<KEY>"))

    }
};

在的onDestroy()

in onDestroy()

unregisterReceiver(uiUpdated);

这篇关于更新的TextView与本地服务的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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