0

I am developing an android application which will start a service.The service will execute some code after every fixed interval of time and end the result to the activity.The activity should display the result to the user.

First I tried it with a thread. For the service to execute after a fixed interval I create a thread - execute the code, get the result - send this result to activity for display - put the thread to sleep for a some fixed time interval. But it is not working as expected.The code is executed by the thread. The thread goes to sleep and then the result is sent to the activity at the end after the sleep time interval. The requirement is the UI must be immediately updated after the result is obtained by the thread code execution.

I have also tried using Timer and TimerTask. But it gives the same result as above. Please help me on this.

Service class

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    td = new ThreadDemo();
    td.start();
}
private class ThreadDemo extends Thread  
{
    @Override
        public void run()
        {
            super.run();
            String result = //code executes here and returns a result
            sendMessageToUI(result);  //method that will send result to Activity
            ThreadDemo.sleep(5000);
        }
}

private void sendMessageToUI(String strMessage)
{
    Bundle b = new Bundle();
    b.putString(“msg”, strMessage);
    Message msg = Message.obtain(null, 13);
    msg.setData(b);
}

Activity class

public class MyActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }
    class IncomingHandler extends Handler 
    {
        @Override
        public void handleMessage(Message msg)
        {
            System.out.println("in ui got a msg................");
            switch (msg.what)
            {
                case 13:
                    System.out.println("setting status msg..............");
                    String str1 = msg.getData().getString("msg");
                    textview.setText(str1);
                    break;      
            }
        }
    }
}
ShwetaK
  • 180
  • 4
  • 14

3 Answers3

0

The broadcast receiver is much better to use in your case.

Register it in activity and send broadcasts from your run target

Yahor10
  • 2,123
  • 1
  • 13
  • 13
0

Use LocalBroadcastManager. Your service will create a local broadcast and it will be available to your application. You can write a proper handler for that notification in your application and update the user interface accordingly.

Put the following code in your service

Intent i = new Intent("NotificationServiceUpdate");
        LocalBroadcastManager.getInstance(this).sendBroadcast(i);

In your application, put the following in the activity which you want to update

     LocalBroadcastManager.getInstance(this).registerReceiver(
                mMessageReceiver, new IntentFilter("NotificationServiceUpdate"));

Now your activity will get a notification whenever the service broadcasts the completion of task. You can further make this broadcast private to your application.

Murtuza Kabul
  • 6,438
  • 6
  • 27
  • 34
  • Thanks for the quick reply.But LocalBroadcastManager requires a higher API level.And the requirement is to use 2.3.3. I am interested in knowing the behavior of Android thread. Why does it not work as expected? i.e. update UI and then sleep. Thanks. – ShwetaK Oct 16 '12 at 13:31
0

see here my answer about the ScheduledExecutor:

how to call a section of code every second in background in Android

and use the message to UI just like you're currently doing.

Community
  • 1
  • 1
Budius
  • 39,391
  • 16
  • 102
  • 144