Android(java)学习笔记172:服务(service)之绑定服务调用服务里面的方法 (采用接口隐藏代码内部实现)

1. 接口

接口可以隐藏代码内部的细节,只暴露程序员想暴露的方法

2. 利用上面的思想优化之前的案例:服务(service)之绑定服务调用服务里面的方法,如下:

(1)这里MainActivity.java:

 1 package com.itheima.bind;
 2 
 3 
 4 import android.app.Activity;
 5 import android.content.ComponentName;
 6 import android.content.Intent;
 7 import android.content.ServiceConnection;
 8 import android.os.Bundle;
 9 import android.os.IBinder;
10 import android.view.View;
11 
12 public class MainActivity extends Activity {
13     IService myBinder;
14 
15     @Override
16     protected void onCreate(Bundle savedInstanceState) {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19     }
20     
21     protected void onDestory(Bundle savedInstanceState) {
22         super.onDestory();
23         -----;//解绑服务
24     }
25 
26     /**
27      * 绑定服务,获取服务里面的小蜜,间接的调用服务里面的方法。
28      * @param view
29      */
30     public void bind(View view){
31         Intent intent = new Intent(this,DemoService.class);
32         //intent 意图
33         //conn 服务的通讯频道
34         //1 服务如果在绑定的时候不存在,会自动创建
35         System.out.println("1.采用bind的方式开启服务");
36         bindService(intent, new MyConn(), BIND_AUTO_CREATE);
37     }
38     
39     /**
40      * 解绑服务
41      * @param view
42      */
43     public void unbind(View view){
44        
45         System.out.println("解绑服务");
46         if(myBinder != null) {
47             unbindService(new MyConn());
48             myBinder = null;
49         }
50    
51     }
52     
53     /**
54      * 服务连接成功的通讯频道
55      *
56      */
57     private class MyConn implements ServiceConnection{
58         //当服务被成功连接的时候调用的方法
59         @Override
60         public void onServiceConnected(ComponentName name, IBinder service) {
61             System.out.println("3. 得到了服务的一个连接,通讯频道,获取到服务内部的小蜜");
62             myBinder = (IService) service;
63             System.out.println("4.把ibinder强制类型转化成小蜜 MyBinder");
64         }
65         //当服务失去连接的时候调用的方法
66         @Override
67         public void onServiceDisconnected(ComponentName name) {
68             
69         }
70     }
71     
72     /**
73      * 调用服务里面的方法。
74      * @param view
75      */
76     public void call(View view){
77         System.out.println("5.利用mybinder间接的调用服务的方法");
78         myBinder.callMethodInService(600);
79     }
80 }

(2)其中的DemoService.java:

 1 package com.itheima.bind;
 2 
 3 import android.app.Service;
 4 import android.content.Intent;
 5 import android.os.Binder;
 6 import android.os.IBinder;
 7 import android.widget.Toast;
 8 
 9 public class DemoService extends Service {
10 
11     /**
12      * 在服务被绑定的时候调用的方法
13      * 
14      * IBinder 服务内部的小蜜 
15      */
16     @Override
17     public IBinder onBind(Intent intent) {
18         System.out.println("2. 服务如果成功绑定会执行onbind,返回服务内部的小蜜 mybinder");
19         return new MyBinder();
20     }
21     /**
22      * 服务内部的小蜜,可以调用服务的方法
23      *
24      */
25     private class MyBinder extends Binder implements IService{
26         /**
27          * 调用服务的方法。
28          * @param money 钱
29          */
30         public void callMethodInService(int money){
31             if(money>500){
32                 methodInService();
33             }else{
34                 Toast.makeText(DemoService.this, "这点钱还想办事呀?", 0).show();
35             }
36         }
37         public void 打麻将(){
38             Toast.makeText(DemoService.this, "一起打麻将", 0).show();
39         }
40         public void 洗桑拿(){
41             Toast.makeText(DemoService.this, "一起洗桑拿", 0).show();
42         }
43     }
44     
45     
46 
47     /**
48      * 服务里面的方法
49      */
50     public void methodInService(){
51         Toast.makeText(this, "哈哈,我是服务的方法,被你调用了。", 0).show();
52     }
53     
54     @Override
55     public void onCreate() {
56         System.out.println("服务被创建了");
57         super.onCreate();
58     }
59     @Override
60     public void onDestroy() {
61         System.out.println("服务被销毁了。");
62         super.onDestroy();
63     }
64 }

(3)接口IService.java:

package com.itheima.bind;

/**
 * 定义的接口,暴露一个给钱办事的方法。
 */
public interface IService {
    public void callMethodInService(int money);
}

其他文件代码不变

(4)工程一览图:

原文地址:https://www.cnblogs.com/hebao0514/p/4801125.html