Android 之简单 AIDL 客户端和服务端 附源码
2017-08-02 10:43
281 查看
AIDL 的全称是 Android Interface Definition Language(即 Android 接口定义语言)
![](https://img-blog.csdn.net/20170802104003582?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3Fld3ExOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
/ * * * * 在service 中,先运行一下 等待生成 则可以调用接口了
点击 Build -> Make Project,然后等待构建完成。
![](https://img-blog.csdn.net/20170802104139279?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3Fld3ExOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
/ * * * * 在客户端调用
相同的位置相同的地方 用相同的所有
![](https://img-blog.csdn.net/20170802161624460?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3Fld3ExOQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
/ * * * * * Service
客户端
代码下载链接这里写链接内容
在同一个app中
/ * * * * 首先在main 文件下定义 aidl 文件// IMyAidlInterface.aidl package com.test.service; // Declare any non-default types here with import statements interface IMyAidlInterface { /** * Demonstrates some basic types that you can use as parameters * and return values in AIDL. */ void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString); int add(int a,int b); int minus(int a,int b ); }
/ * * * * 在service 中,先运行一下 等待生成 则可以调用接口了
点击 Build -> Make Project,然后等待构建完成。
public class TestService extends Service { private static final String TAG = TestService.class.getSimpleName(); @Override public IBinder onBind(Intent intent) { return mBinder; } private final IMyAidlInterface.Stub mBinder = new IMyAidlInterface.Stub() { @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } @Override public int add(int a, int b) throws RemoteException { return a+b; } @Override public int minus(int a, int b) throws RemoteException { return a-b; } }; }
/ * * * * 在客户端调用
public class MainActivity extends Activity { public static final String TAG = MainActivity.class.getSimpleName(); public static final int MEG_SUM = 0x1100; private IMyAidlInterface.Stub mBinder; private Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case MEG_SUM: Toast.makeText(MainActivity.this, "接收到service消息==>" + msg.arg1, Toast.LENGTH_SHORT).show(); } } }; ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { mBinder = (IMyAidlInterface.Stub) service; try { msg = mBinder.add(3, 3); } catch (RemoteException e) { e.printStackTrace(); } Toast.makeText(MainActivity.this, "Activity发送消息", Toast.LENGTH_SHORT).show(); Message message = Message.obtain(); message.what = MEG_SUM; message.arg1 = msg; mHandler.sendMessage(message); } @Override public void onServiceDisconnected(ComponentName name) { } }; private int msg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(this, TestService.class); bindService(intent, connection, Context.BIND_AUTO_CREATE); } @Override protected void onDestroy() { super.onDestroy(); unbindService(connection); } }
在不同一个app中
/ * * * * * * Client * * * * * * *Service相同的位置相同的地方 用相同的所有
/ * * * * * Service
public class TestService extends Service { private static final String TAG = TestService.class.getSimpleName(); private IMyAIDL iMyAIDL; @Override public IBinder onBind(Intent intent) { return iMyAIDL; } @Override public void onCreate() { super.onCreate(); iMyAIDL = new IMyAIDL(); } public class IMyAIDL extends IMyAidlInterface.Stub{ @Override public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException { } @Override public int add(int a, int b) throws RemoteException { return a+b; } @Override public int minus(int a, int b) throws RemoteException { return a-b; } } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.service"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".TestService" android:enabled="true" android:exported="true"> <!--用于客户端调用--> <intent-filter> <action android:name="android.intent.action.MyService"/> </intent-filter> </service> </application> </manifest>
客户端
import com.test.service.IMyAidlInterface; public class MainActivity extends Activity { public static final String TAG = MainActivity.class.getSimpleName(); public static final int MEG_SUM = 0x1100; private Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case MEG_SUM: Toast.makeText(MainActivity.this, "接收到service消息==>" + msg.arg1, Toast.LENGTH_SHORT).show(); } } }; ServiceConnection connection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service); try { msg= iMyAidlInterface.add(5,3); Message message = Message.obtain(); message.what = MEG_SUM; message.arg1 = msg; mHandler.sendMessage(message); Log.i(TAG, "onServiceConnected: "+msg); } catch (RemoteException e) { e.printStackTrace(); } } @Override public void onServiceDisconnected(ComponentName name) { } }; private int msg; private IMyAidlInterface iMyAidlInterface; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(); intent.setAction("android.intent.action.MyService"); intent.setPackage("com.test.service"); bindService(intent, connection, Context.BIND_AUTO_CREATE); } @Override protected void onDestroy() { super.onDestroy(); unbindService(connection); } }
代码下载链接这里写链接内容
相关文章推荐
- android AIDL简单入门(客户端与服务端)
- 简单的Android服务端和客户端登录交互(服务端)
- 简单的android客户端servlet服务端的交互
- 简单java Web Service服务端和Android客户端
- Android aidl项目中服务端与客户端aidl文件不一致引起的问题
- 婚恋交友背背恋app源码+android客户端+java服务端源码
- Android AIDL 客户端和服务端配置
- 瞎聊!Android之aidl进程之间传递对象简单分析加 源码
- WCF服务端与使用HttpURLConnection的Android客户端简单示例
- Android网络编程之TCP通信----简单客户端与服务端通信
- Android发送图片到指定邮箱(仅客户端简单实现,不需服务端配合)
- android jsoup解析服务端数据客户端源码讲解。
- Android客户端与服务端(jsp)之间json的传输与解析【附效果图附源码】
- node.js版简单客户端和服务端通讯源码
- Android学之客户端服务端Socket简单通信
- android客户端和servlet服务端的简单登录实现
- 客户端与服务器SSL双向认证(客户端:Android-服务端:vc)-含源码
- android客户端 与javaweb服务端的简单理解
- golang grpc服务端客户端简单源码实现
- WCF服务端与使用HttpClient的Android客户端简单示例