不断更新android Textview

时间:2015-10-31 05:10:36

标签: android android-layout textview android-service

我正在开发一个Android应用程序,它有一个key(){ read -p "Paste Your Private Key Here: " privatekey while read $privatekey; do echo $privatekey > ~/.ssh/id_rsa; done chmod 600 ~/.ssh/id_rsa chown work:work ~/.ssh/id_rsa echo "Your SSH Key Authentication is setup!" } 类和activity类。在service中,将每10毫秒收到一次连续批量service。我需要使用这些批量数据不断更新data (1090 bytes)。从连续后台服务更新text view的推荐方法是什么?

服务类

Text view

4 个答案:

答案 0 :(得分:1)

用户LocalBroadcastManager

counter = {}

with open('tale_of_two_cities_ascii.txt') as infile:
    for line in infile:
        for word in line.strip():
            if len(word) != 5: continue
            if word not in counter: counter[word] = 0
            counter[word] += 1

words = sorted(counter, key=counter.__get__)
print("The five most common words are:", ','.join(words[-5:]))
print("The five least common words are:", ','.join(words[:5]))

在MainActivity中

public void Receivepatientattributes(byte[] readBuf, int len) {
    String total_data = "";
    total_data = bytetohex(readBuf, len);

    Intent intent = new Intent("update-text");
    // add data
    intent.putExtra("message", total_data);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

Reference

答案 1 :(得分:1)

如果您想使用计划和计时器任务,那么您可以See My Answer

要解决当前问题,请遵循以下说明。 假设您的活动有广播接收器

onResume()

然后你覆盖你的广播接收者将被注册的方法onPause(),以及@Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); IntentFilter intentFilter = new IntentFilter( "android.intent.action.MAIN"); mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //extract your message from intent String msg_for_me = intent.getStringExtra("YOUR_MESSAGE"); //log your message value Log.i("MyTag", msg_for_me); } }; //registering your receiver this.registerReceiver(mReceiver, intentFilter); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); //unregister your receiver this.unregisterReceiver(this.mReceiver); } 你的接收者将在哪里取消注册:

android.intent.action.MAIN

此处广播接收器通过Receivepatientattributes进行过滤,来自服务的消息将使用此过滤器广播BroadCast

现在您的方法public void Receivepatientattributes(byte[] readBuf, int len) { String total_data = ""; total_data = bytetohex(readBuf, len); Intent i = new Intent("android.intent.action.MAIN").putExtra("YOUR_MESSAGE", total_data); this.sendBroadcast(i); } 会喜欢这样:

{{1}}

多数民众赞成。 :)

答案 2 :(得分:0)

您可以使用Timer继续更新文本视图。

每次使用最新值运行服务时,在首选项中设置值。

现在在Timer中从首选项获取该值并使用该值更新TextView。

以下是一些代码:

class UpdateTimeTask extends TimerTask {
   public void run() {

   textview.setText("updated value");
   }
}

设置onCreate();

    Timer timer = new Timer();
    UpdateTimeTask  UpdateTimeTask = new UpdateTimeTask ();
    timer.schedule(UpdateTimeTask, 1000);

答案 3 :(得分:0)

使用处理程序,因为处理程序允许从其他后台线程与UI线程通讯回来。

boolean handlerStop = false;

    void handleHandler(){
        Handler handler =new Handler();
        final Runnable r = new Runnable() {
            public void run() {
                handler.postDelayed(this, 30000);
                if(!handlerStop) {
                    updateTextView() //update your text with other thread like asyncronous thread
                }
            }
        };
        handler.postDelayed(r, 0000);
    }

    @Override
    public void onResume() {
        super.onResume();
        handlerStop=false;
        handleHandler();
    }

    @Override
    public void onPause() {
        super.onPause();
        handlerStop=true;
        handleHandler();
    }

    @Override
    public void onStop() {
        super.onStop();
        handlerStop=true;
        handleHandler();
    }