开发者

android service question

开发者 https://www.devze.com 2022-12-31 07:51 出处:网络
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 numbe

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!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号