views:

40

answers:

1

Hello,

I have a class, MyService. This is my Android service. I wanna generate two random numbers, and this service should return the sum of these numbers. How can i do that? So , i should generate the numbers in

 public void onStart(Intent intent, int startid) {
  //Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
  // here i generate the numbers (random) and i compute the sum , nr1+nr2.

 }

Then, how can i return the result to the main activity, and display the sum of the numbers as an alert (for example), or in an edit box when a button is pressed? So, in the main class i have

    public void onClick(View src) {
  switch (src.getId()) {
                //start the service
  case R.id.buttonStart: 
   Log.d(TAG, "onClick: starting srvice");
   startService(new Intent(this, MyService.class));
   break;
  case R.id.buttonStop:
   Log.d(TAG, "onClick: stopping srvice");
   stopService(new Intent(this, MyService.class));
   break;
  }

The question is how/where can i return the result of that service (that nr1+nr2), and how should i display the result?

Thanks!

A: 

Please refer to this question http://stackoverflow.com/questions/2468874/how-can-i-update-information-in-an-android-activity-from-a-background-service

Alex Volovoy
So i don't know in my case how to apply... i need to generate just two numbers, calculate the sum, and that sum to display somewhere.. regarding to the code i posted, where should i write that code?Thanks
qwerty
I saw that example from the link you gave me, but it's a little complicated.. so i need something easier, a service to calculate the sum of 2 numbers
qwerty
You don't need a service - you need a worker thread.http://developer.android.com/guide/appendix/faq/commontasks.html#threading
Alex Volovoy
And using service is it possible? If yes, how? just asking.. :)
qwerty
It is possible. In your case not needed but possible. I think Mark's answer in the link about give more than enough details and choices of how to accomplish that. Beyond that you need to read.
Alex Volovoy
More exactly, which link?
qwerty
http://stackoverflow.com/questions/2468874/how-can-i-update-information-in-an-android-activity-from-a-background-service
Alex Volovoy