码迷,mamicode.com
首页 > 其他好文 > 详细

AIDL--------应用之间的通信接口

时间:2015-09-19 21:16:34      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

在下面例子中04Service中添加aidl包包里定义好接口 接口文件名后缀为.aidl

package com.example.aidl;

interface IRemoteService
{
void print(String msg);
String getName();

}

在04 client中也有这样一个包 这个包就成为一个接口   Service里实现接口  client中可以调用注意

Service中定义好service的action  client中绑定服务时用这个action

<service android:name=".RemoteService">
<intent-filter >
<action android:name="com.example.service04aidl.RemoteService"/>
</intent-filter>
</service>

绑定时用到

bindService(new Intent("com.example.service04aidl.RemoteService"),conn,BIND_AUTO_CREATE);

客户端每次绑定都会创建一个与客户端activity绑定的Service  activity销毁服务也会销毁

技术分享
 1 package com.example.service04_client;
 2 
 3 import android.app.Activity;
 4 import android.content.ComponentName;
 5 import android.content.Intent;
 6 import android.content.ServiceConnection;
 7 import android.os.Bundle;
 8 import android.os.IBinder;
 9 import android.os.RemoteException;
10 import android.view.View;
11 import android.widget.Toast;
12 
13 import com.example.aidl.IRemoteService;
14 
15 public class MainActivity extends Activity {
16 
17     IRemoteService remoteService;
18     ServiceConnection conn=new ServiceConnection() {        
19         @Override
20         public void onServiceDisconnected(ComponentName name) {
21 
22         }
23         
24         @Override
25         public void onServiceConnected(ComponentName name, IBinder service) {
26             remoteService=IRemoteService.Stub.asInterface(service);    
27         }
28     };
29     
30     @Override
31     protected void onCreate(Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33         setContentView(R.layout.activity_main);
34         
35         bindService(new Intent("com.example.service04aidl.RemoteService"),conn,BIND_AUTO_CREATE);
36     }
37     public void print(View v) throws RemoteException
38     {
39         if(remoteService!=null)
40         remoteService.print("hello,service");
41     }
42     public void getRemoteName(View v) throws RemoteException
43     {
44         if(remoteService!=null)
45         Toast.makeText(getApplicationContext(), remoteService.getName(), 1).show();
46     }
47     
48 }
04Service—mainActivity
技术分享
 1 package com.example.service04aidl;
 2 
 3 import com.example.aidl.IRemoteService;
 4 
 5 import android.app.Service;
 6 import android.content.Intent;
 7 import android.os.IBinder;
 8 import android.os.RemoteException;
 9 import android.util.Log;
10 
11 public class RemoteService extends Service {
12 
13     private IRemoteService.Stub stub=new IRemoteService.Stub() {    
14         @Override
15         public void print(String msg) throws RemoteException {
16             Log.i("debug", "RemoteService---"+msg);
17         }
18         @Override
19         public String getName() throws RemoteException {
20             return "RemoteService";
21         }
22     };
23     @Override
24     public IBinder onBind(Intent intent) {
25         // TODO Auto-generated method stub
26         return stub;
27     }
28 
29 }
04Service--RemoteService
技术分享
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.service04aidl"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="17" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name="com.example.service04aidl.MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25         <service android:name=".RemoteService">
26             <intent-filter >
27                 <action android:name="com.example.service04aidl.RemoteService"/>
28             </intent-filter>
29         </service>
30     </application>
31 
32 </manifest>
04----xml
技术分享
 1 package com.example.service04_client;
 2 
 3 import android.app.Activity;
 4 import android.content.ComponentName;
 5 import android.content.Intent;
 6 import android.content.ServiceConnection;
 7 import android.os.Bundle;
 8 import android.os.IBinder;
 9 import android.os.RemoteException;
10 import android.view.View;
11 import android.widget.Toast;
12 
13 import com.example.aidl.IRemoteService;
14 
15 public class MainActivity extends Activity {
16 
17     IRemoteService remoteService;
18     ServiceConnection conn=new ServiceConnection() {        
19         @Override
20         public void onServiceDisconnected(ComponentName name) {
21 
22         }
23         
24         @Override
25         public void onServiceConnected(ComponentName name, IBinder service) {
26             remoteService=IRemoteService.Stub.asInterface(service);    
27         }
28     };
29     
30     @Override
31     protected void onCreate(Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33         setContentView(R.layout.activity_main);
34         
35         bindService(new Intent("com.example.service04aidl.RemoteService"),conn,BIND_AUTO_CREATE);
36     }
37     public void print(View v) throws RemoteException
38     {
39         if(remoteService!=null)
40         remoteService.print("hello,service");
41     }
42     public void getRemoteName(View v) throws RemoteException
43     {
44         if(remoteService!=null)
45         Toast.makeText(getApplicationContext(), remoteService.getName(), 1).show();
46     }
47     
48 }
client

 

AIDL--------应用之间的通信接口

标签:

原文地址:http://www.cnblogs.com/bimingcong/p/4822175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!