在Activity中调用本地服务(Service)中的方法

如何在Activity中调用服务服务中的方法呢,下面我们用一个例子简单的说一下
 
需求:服务中有一个方法,返回值为String 内容为“张三”,现在我们通过调用服务中的方法把“张三”显示在主Activity上。
新建一个服务类MyService,继承Service

01. package cn.cbd.service;
02. import android.app.Service;
03. import android.content.Intent;
04. import android.os.Binder;
05. import android.os.IBinder;
06. public class MyService extends Service
07. {
08. @Override
09. public void onCreate()
10. {
11. super.onCreate();
12. }
13. @Override
14. public IBinder onBind(Intent intent)
15. {
16. return new MyBinder();
17. }
18. @Override
19. public void onDestroy()
20. {
21. super.onDestroy();
22. }
23. public class MyBinder extends Binder
24. {
25. public String getname()
26. {
27. return getName();
28. }
29. }
30. /**
31. * 在服务中自定义一个方法,我们要在activity中调用这个方法
32. *
33. * @return
34. */
35. public String getName()
36. {
37. return "张三";
38. }
39. }
在上面我们看到,getName()这个方法是我们在Service中定义的,现在我们要在主Activity中调用这个方法。怎么调用呢,上面MyService中有一个onBind(Intent intent)方法 返回值类型为IBinder activity和service就是通过这个方法来进行通信。 所以呢 我们为了方便起见,就自定义了一个类MyBinder继承Binder ,在MyBinder类中定义一个方法getname(),返回getName()的值。
Service也是android四大组件之一,所以呢,不要忘记在清单文件中对其进行配置
 

 

下面我们看Activity这一面:

01. package cn.cbd.service;
02. import android.app.Activity;
03. import android.content.ComponentName;
04. import android.content.Context;
05. import android.content.Intent;
06. import android.content.ServiceConnection;
07. import android.os.Bundle;
08. import android.os.IBinder;
09. import android.view.View;
10. import android.view.View.OnClickListener;
11. import android.widget.Button;
12. import android.widget.TextView;
13. public class ServiceTestActivity extends Activity
14. {
15. private Button btn_bindService;
16. private TextView tv_show;
17. private MyServiceConnection conn;
18. private MyService.MyBinder myBinder;
19. public void onCreate(Bundle savedInstanceState)
20. {
21. super.onCreate(savedInstanceState);
22. setContentView(R.layout.main);
23. btn_bindService = (Button) findViewById(R.id.btn_bindService);
24. tv_show = (TextView) findViewById(R.id.tv_show);
25. btn_bindService.setOnClickListener(new OnClickListener()
26. {
27. public void onClick(View v)
28. {
29. // 开启服务
30. Intent service = new Intent(ServiceTestActivity.this,
31. MyService.class);
32. conn = new MyServiceConnection();
33. // startService(service)只能单纯的开启一个服务,要想调用服务服务中的方法,必须用bindService和unbindService
34. // startService(service);
35. bindService(service, conn, Context.BIND_AUTO_CREATE);
36. }
37. });
38. }
39. /**
40. * 自定义一个类,实现ServiceConnection接口,并重写其两个方法
41. * @author Administrator
42. *
43. */
44. public class MyServiceConnection implements ServiceConnection
45. {
46. //当绑定服务成功的时候会调用此方法
47. public void onServiceConnected(ComponentName name, IBinder service)
48. {
49. //得到MyService.MyBinder对象,我们通过这个对象来操作服务中的方法
50. myBinder = (MyService.MyBinder) service;
51. //调用服务中的getname()方法并把值设置到TextView上进行显示
52. tv_show.setText(myBinder.getname());
53. }
54. public void onServiceDisconnected(ComponentName name)
55. {
56. }
57. }
58. @Override
59. protected void onDestroy()
60. {
61. // 在程序销毁的时候要对服务解除绑定
62. unbindService(conn);
63. super.onDestroy();
64. }
65. }


效果图:
 

\
 

从运行结果上我们可以看出,我们已经通过调用服务中的getName()方法把“张三”显示出来了。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值