绑定服务调用本地服务中的方法

如果想调用服务中的方法, 通过startService()是做不到的, 这时需要用bindService来解决.

下面的demo是在Activity中调用Service中的自定义方法---methodInService

这个demo可以解决在项目开发中调用service里的数据。

这里在service中使用到了代理模式。这是为了,给service组件和activity组件中间添加一个中间人。

通过代理来传递数据。也就是binder对象。这个代理就是接口IService

 

Service中的代码如下:

 

 1 public class DemoService extends Service {
 2 
 3     /**
 4      * 内部的小蜜,可以调用服务内部的方法
 5      * 将小蜜私有了,建一个接口,提供对外访问的方法
 6      */
 7     private class Xiaomi extends Binder implements IService{
 8         //通过这个方法调用服务中的方法
 9         //重写接口中的抽象方法
10         public void callMethodInService(int money){
11             if(money > 100){
12                 //这里调用了服务里的方法
13                 methodInService();
14             }else{
15                 System.out.println("这点钱还想办事啊?");
16             }
17         }
18         
19         public void 洗桑拿(){
20             System.out.println("一起洗桑拿");
21         }
22     }
23 
24     /**
25      * 在执行bindService时调用该方法
26      * IBinder相当于内部的小蜜
27      */
28     @Override
29     public IBinder onBind(Intent intent) {
30         System.out.println("2,服务如果成功绑定,会执行onBind方法,返回IBinder小蜜");
31         return new Xiaomi();
32     }
33 
34     @Override
35     public void onCreate() {
36         System.out.println("服务已经开启了");
37 
38     }
39 
40     @Override
41     public void onDestroy() {
42         System.out.println("服务已经关闭了");
43         super.onDestroy();
44     }
45     
46     /**
47      * 服务中的方法
48      */
49     public void methodInService(){
50         System.out.println("6,你成功调用了服务中的方法");
51     }
52 
53 }

 

IService接口代码如下:

 

1 public interface IService {
2 
3     public void callMethodInService(int money);
4     
5 }

 

Activity中的代码如下:

 

 1 public class MainActivity extends Activity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_main);
 7     }
 8     
 9     MyConn conn;
10     //创建接口的对象
11     IService myBinder;
12 
13     //绑定服务
14     public void bindService(View view) {
15         System.out.println("1,绑定服务");
16         Intent intent = new Intent(this, DemoService.class);
17         conn = new MyConn();
18         bindService(intent, conn, BIND_AUTO_CREATE);
19     }
20 
21     //解绑服务
22     public void unbindService(View view) {
23 
24         unbindService(conn);
25     }
26 
27     /**
28      * 调用服务里的方法
29      */
30     public void callService(View view) {
31         //通过内部的小蜜可以间接调用服务里的方法
32         System.out.println("5,利用myBinder间接调用服务里的方法");
33         myBinder.callMethodInService(500);
34     }
35     
36     
37     /**
38      * 服务成功连接的通讯频道
39      */
40     private class MyConn implements ServiceConnection{
41 
42         //服务成功连接时
43         @Override
44         public void onServiceConnected(ComponentName name, IBinder service) {
45             System.out.println("3,得到了服务的链接,获取到了小蜜");
46             myBinder = (IService) service;
47             System.out.println("4,将IBinder强制转换为小蜜myBinder");
48         }
49 
50         //服务连接不成功时
51         @Override
52         public void onServiceDisconnected(ComponentName name) {
53             
54         }
55     }
56 }

 

关键代码已经用红色标出

 

在这个demo中, Activity想直接调用服务中的方法是办不到的, 于是可以定义一个代理Iservice接口,

让Service中的内部类Xiaomi来实现该接口, 并重写抽象方法, 这样就可以在Activity中调用了.

 

posted @ 2016-03-18 21:48  王浩宇blog  阅读(1539)  评论(0编辑  收藏  举报