博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AIDL--------应用之间的通信接口
阅读量:5217 次
发布时间:2019-06-14

本文共 4877 字,大约阅读时间需要 16 分钟。

在下面例子中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         @Override20         public void onServiceDisconnected(ComponentName name) {21 22         }23         24         @Override25         public void onServiceConnected(ComponentName name, IBinder service) {26             remoteService=IRemoteService.Stub.asInterface(service);    27         }28     };29     30     @Override31     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 RemoteException38     {39         if(remoteService!=null)40         remoteService.print("hello,service");41     }42     public void getRemoteName(View v) throws RemoteException43     {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         @Override15         public void print(String msg) throws RemoteException {16             Log.i("debug", "RemoteService---"+msg);17         }18         @Override19         public String getName() throws RemoteException {20             return "RemoteService";21         }22     };23     @Override24     public IBinder onBind(Intent intent) {25         // TODO Auto-generated method stub26         return stub;27     }28 29 }
04Service--RemoteService
1 
2
6 7
10 11
16
19
20
21 22
23
24 25
26
27
28
29
30 31 32
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         @Override20         public void onServiceDisconnected(ComponentName name) {21 22         }23         24         @Override25         public void onServiceConnected(ComponentName name, IBinder service) {26             remoteService=IRemoteService.Stub.asInterface(service);    27         }28     };29     30     @Override31     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 RemoteException38     {39         if(remoteService!=null)40         remoteService.print("hello,service");41     }42     public void getRemoteName(View v) throws RemoteException43     {44         if(remoteService!=null)45         Toast.makeText(getApplicationContext(), remoteService.getName(), 1).show();46     }47     48 }
client

 

转载于:https://www.cnblogs.com/bimingcong/p/4822175.html

你可能感兴趣的文章
菜根谭#60
查看>>
Acne Scars Treatment - Probably the Best Acne Scars Treatment Available
查看>>
Linux(Ubuntu)下MySQL的安装
查看>>
Oracle学习DayThree
查看>>
Android事件处理
查看>>
dbca静默建库
查看>>
将MySQL数据迁移到Oracle
查看>>
DedeCms V5.6 plus/advancedsearch.php 任意sql语句执行漏洞
查看>>
Web前端开发JQuery框架(5)
查看>>
软件开发高手须掌握的4大SQL精髓语句(二)
查看>>
安装好oracle后,打开防火墙遇到的问题!
查看>>
ios通过代码方式获取crash日志
查看>>
MongoDB 之 "$" 的奇妙用法 MongoDB - 5
查看>>
hdu 4398 STL
查看>>
OSINT系列:威胁信息挖掘ThreatMiner
查看>>
ASP.NET运行机制之一般处理程序(ashx)
查看>>
第二次作业
查看>>
BZOJ1191: [HNOI2006]超级英雄Hero
查看>>
BZOJ3506: [Cqoi2014]排序机械臂
查看>>
怎样提升 RailS 应用的性能?
查看>>