1

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!

qwerty
  • 1,967
  • 6
  • 22
  • 22

1 Answers1

0

Please refer to this question How can I update information in an Android Activity from a background Service

Community
  • 1
  • 1
Alex Volovoy
  • 67,778
  • 13
  • 73
  • 54
  • 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 May 25 '10 at 14:44
  • 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 May 25 '10 at 14:54
  • You don't need a service - you need a worker thread. http://developer.android.com/guide/appendix/faq/commontasks.html#threading – Alex Volovoy May 25 '10 at 15:29
  • And using service is it possible? If yes, how? just asking.. :) – qwerty May 25 '10 at 15:43
  • 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 May 25 '10 at 15:57
  • http://stackoverflow.com/questions/2468874/how-can-i-update-information-in-an-android-activity-from-a-background-service – Alex Volovoy May 26 '10 at 12:50