AIDL 的理解与使用(一种android内部进程通信接口的描述语言)

来源:互联网 发布:网络保密管理制度 编辑:程序博客网 时间:2024/05/17 21:38

通过

intent i=new Intent();

intent.setcomponent(newComponentName("包名","包名类名"));

第一步,
需要修改service1项目中aidl,增加一个方法。

?
1
2
3
4
5
6
7
8
<code actionscript=""class="hljs">packagecom.example.service1.aidl; 
 
interfaceIMyService { 
 
    voidbasicType();
 
    voidsetName(String name);
}</code>

setName用于存储name的方法。
然后clear项目

第二步,
此时,我们service类中的onBind方法需要实现新接口。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<codeclass="hljs"java="">packagecom.example.service1;
 
importandroid.app.Service;
importandroid.content.Intent;
importandroid.os.IBinder;
importandroid.os.RemoteException;
 
importcom.example.service1.aidl.IMyService;
 
publicclass MyService extendsService {
 
    privateString serviceName = 默认名字;
    privateboolean running;
 
    @Override
    publicIBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        returnnew IMyService.Stub() {
 
            @Override
            publicvoid basicType() throwsRemoteException {
                // TODO Auto-generated method stub
 
            }
 
            @Override
            publicvoid setName(String name) throwsRemoteException {
                serviceName = name;
            }
        };
    }
 
    @Override
    publicvoid onCreate() {
        running = true;
        newThread() {
            publicvoid run() {
                while(running) {
                    try{
                        Thread.sleep(1000);
                    }catch(InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    System.out.println(serviceName);
                }
            };
        }.start();
        super.onCreate();
    }
 
    @Override
    publicvoid onDestroy() {
        super.onDestroy();
 
        running = false;
    }
 
}
</code>

代码27到29行,接收到name并且放入全局变量中,提供给onCreate方法输出。

第三步,
将service1项目中aidl拷贝到service2项目中,并且包名要一致,
这里写图片描述

第四步,
修改service2应用activity布局,增加一个text域,和一个按钮。用于将text中的信息提交到service1项目的service中。
这里写图片描述

第五步,
修改service2项目中activity,增加与service1的通信,

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<codeclass="hljs"java="">packagecom.example.service2;
 
importcom.example.service1.aidl.IMyService;
 
importandroid.app.Activity;
importandroid.content.ComponentName;
importandroid.content.Context;
importandroid.content.Intent;
importandroid.content.ServiceConnection;
importandroid.os.Bundle;
importandroid.os.IBinder;
importandroid.os.RemoteException;
importandroid.view.Menu;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.TextView;
 
publicclass MainActivity extendsActivity implementsOnClickListener,
        ServiceConnection {
 
    Intent serviceIntent;
 
    privateIMyService imyService = null;
 
    TextView t;
 
    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        serviceIntent = newIntent();
        serviceIntent.setComponent(newComponentName(com.example.service1,
                com.example.service1.MyService));
 
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);
 
        findViewById(R.id.button5).setOnClickListener(this);
 
        t = (TextView) findViewById(R.id.textView1);
    }
 
    @Override
    publicboolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        returntrue;
    }
 
    @Override
    publicvoid onClick(View v) {
        switch(v.getId()) {
        caseR.id.button1:
            startService(serviceIntent);
            break;
        caseR.id.button2:
            stopService(serviceIntent);
            break;
        caseR.id.button3:
            bindService(serviceIntent,this, Context.BIND_AUTO_CREATE);
            break;
        caseR.id.button4:
            unbindService(this);
            imyService = null;
            break;
        caseR.id.button5:
            if(imyService != null) {
                try{
                    imyService.setName(t.getText().toString());
                }catch(RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
 
    }
 
    @Override
    publicvoid onServiceConnected(ComponentName name, IBinder service) {
        imyService = IMyService.Stub.asInterface(service);
        System.out.println(onServiceConnected);
        System.out.println(service);
 
    }
 
    @Override
    publicvoid onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub
 
    }
 
}
</code>

代码72行,传输数据
代码84行用法imyService = IMyService.Stub.asInterface(service);

运行结果,如图,
这里写图片描述


0 0
原创粉丝点击