当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How to update textview text which is update in service class

问题描述:

I have developed an app with start, pause, resume and finish buttons.

It works properly in the activity using thread and handler.

If the user clicks on the start button a thread is started and displays textviewHH:MM:SS time and the rest of the buttons work correctly as well.

Problem:

If the activity goes to background then how do I update the textview time? I have made services for this task but, how do I take the response from services to UI?

Please, could you give me any idea of how to do it or any other possible solution?

网友答案:

You can create CustomBroadcast

Here is sample code. Try this, it will work..

In YourService.Java

public static final String BROADCAST_ACTION = "com.example.tracking.updateprogress";
intent = new Intent(BROADCAST_ACTION);
sendBroadcast(intent);

In YourActivity.Java

registerReceiver(broadcastReceiver, new IntentFilter(YourService.BROADCAST_ACTION));

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
                //Update Your UI here.. 
                updateUI();      
        }
}

You can also pass data in Intent.

网友答案:

If you create a simple new Thread(new Runnable() {...}) within the Activity you can run UI manipulations with the runOnUiThread(new Runnable() { // your UI modify method }) Acitvity method. If the Activity go to the background the Thread is still running.

分享给朋友:
您可能感兴趣的文章:
随机阅读: